Ahmed Gaber
Ahmed Gaber

Reputation: 87

how to return data in array using laravel

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;
        }

Upvotes: 0

Views: 173

Answers (2)

zlatan
zlatan

Reputation: 3951

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.

Upvotes: 0

sybear
sybear

Reputation: 7784

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

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

Upvotes: 1

Related Questions