3

In laravel when i use DB::select("SELECT * FROM table"); It return a object,but i need a array. So how can i get a array instead of object?

example:

$data = DB::select("SELECT * FROM table");
var_dump($data);
Payer Ahammed
  • 887
  • 1
  • 5
  • 16
  • possible duplicate of [DB query builder toArray() laravel 4](http://stackoverflow.com/questions/20776656/db-query-builder-toarray-laravel-4) – Tim Biegeleisen Sep 18 '15 at 09:10
  • $data itself is an array so $data could not be converted to an array while looping foreach over $data and then converting object to array will throw error take a look at the following question answer http://stackoverflow.com/questions/2022544/can-i-use-stdclass-like-an-array – Basheer Kharoti Sep 18 '15 at 13:27

2 Answers2

3

Please Try this one.

\Illuminate\Support\Facades\DB::setFetchMode(PDO::FETCH_ASSOC);
$values=\Illuminate\Support\Facades\DB::select("select * from  your_table");
\Illuminate\Support\Facades\DB::setFetchMode(PDO::FETCH_CLASS);
var_dump($values);
Md Rashedul Hoque Bhuiyan
  • 10,151
  • 5
  • 30
  • 42
1

It actually returns an array of objects. To make an individual object to be an array just cast it. For example: (array)$data[0]

unxp
  • 318
  • 2
  • 12