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);
2

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

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]

4 Comments

I need a two dimension array not object
Can you explain how you can get a two dimensional array by doing SELECT * when in fact you might get, say 10 records back?
i need a method which return a full array instead of object?
Note: Casting $data to an array is not relevant as it as already an array and casting will not affect it's members. Each of them has to be cast individually or json_encode/json_decode combination has to be used

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.