1

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?

4
  • show your code here Commented Sep 15, 2018 at 10:51
  • 3
    toArray() is correct. You did something else wrong. Commented Sep 15, 2018 at 10:54
  • @AfrazAhmad...show my code Commented Sep 15, 2018 at 11:00
  • 1
    toArray() is correct on an instance of the resulting collection not on Query builder. The select statement has not returned anything from the db, hence chaining toArray() on it would throw error. Commented Sep 15, 2018 at 12:10

3 Answers 3

4

I got result. just change the code

$doctor_result = Doctor::all()->toArray();
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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 ).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.