Arafat Rahman
Arafat Rahman

Reputation: 624

How to retrieve data from a database as an array in Laravel?

Normally data retrieved as an object from database in Laravel when we use get() function. But I want to retrieved data as an array.

In CodeIgnitor we use get_array(). What we use in Laravel ?

I have already tried with toArray(). But no result.

$doctor_result = Doctor::select('*')->toArray();

How to solve that?

Upvotes: 1

Views: 13527

Answers (3)

Ali Akbar Azizi
Ali Akbar Azizi

Reputation: 3516

You can get data from base query builder without any unnecessary transformation.

$doctor_result = Doctor::select('*')->toBase()->get()

This will give you array of stdClass usually ( base on PDO config ).

Upvotes: 0

fahdshaykh
fahdshaykh

Reputation: 692

You can also do like this.

$result = DB::table('tableName')->get();
$resultArray = $result->toArray();

second way, but some tricky.

$resultArray = json_decode(json_encode($result), true);

Upvotes: 1

Arafat Rahman
Arafat Rahman

Reputation: 624

I got result. just change the code

$doctor_result = Doctor::all()->toArray();

Upvotes: 4

Related Questions