0

good day ; I have custom data base helper in laravel framework I have easy question how to return data in array not in objects. her is my function

public static  function  allData($dbName,$tableName,$condition,$data){

            $stattment=
                DB::connection($dbName)
                    ->table($tableName)
                    ->select(['*'])
                    ->whereRaw($condition, $data)
                    ->get();

            return $stattment;

        }

next function

public static function getDataById($dbName,$tableName,$condition,$data)
        {
            $stattment=
                DB::connection($dbName)
                    ->table($tableName)
                    ->select(['*'])
                    ->whereRaw($condition, $data)
                    ->get();
            return $stattment;
        }
1
  • you can use return $stattment->toArray(); Commented Jun 12, 2019 at 12:35

2 Answers 2

1

The query result is a Collection object has a toArray() method.

https://laravel.com/docs/5.8/collections#method-toarray

Sign up to request clarification or add additional context in comments.

Comments

0

Laravel has in-built toArray() method

You can use it like this:

public static  function  allData($dbName,$tableName,$condition,$data){

        $stattment=
            DB::connection($dbName)
                ->table($tableName)
                ->select(['*'])
                ->whereRaw($condition, $data)
                ->get();

        return $stattment->toArray();

    }

Same goes for the other function:

public static function getDataById($dbName,$tableName,$condition,$data)
    {
        $stattment=
            DB::connection($dbName)
                ->table($tableName)
                ->select(['*'])
                ->whereRaw($condition, $data)
                ->get();
        return $stattment->toArray();
    }

Now your $statement will be displayed as array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.