3

I have write that code but i want take an array of this, is this possible?

$results = DB::table('pages')->select('id', 'link')->orderBy('id', 'asc')->get();
1
  • Use toArray() function Commented Apr 30, 2016 at 11:46

2 Answers 2

4

To get laravel query builder result in array check this:

$results = DB::table('pages')->select('id', 'link')->orderBy('id', 'asc')->get()->toArray();

Or

If you prefer to use Query Builder instead of Eloquent here is the solutions

$results = DB::table('pages')->select('id', 'link')->orderBy('id', 'asc')->get();

First Solution

$array = (array) $result;

Second Solution

$array = get_object_vars($result);

Third Solution

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

hope it may help

Sign up to request clarification or add additional context in comments.

2 Comments

I try but... Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN) HELP Call to a member function toArray() on array
Updated to answer @user3211908
2

You can see from the API docs that the get() function returns either a Collection or a Builder.

Laravel collections have the toArray() method (described in the docs) as shown below:

$collection = collect(['name' => 'Desk', 'price' => 200]);

$collection->toArray();

/*
    [
        ['name' => 'Desk', 'price' => 200],
    ]
*/

Therefore, you can do the following:

$results = DB::table('pages')->select('id', 'link')->orderBy('id', 'asc')->get()->toArray();

2 Comments

I try but... Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN) HELP Call to a member function toArray() on array
@user3211908 Can you call dd(DB::table('pages')->select('id', 'link')->orderBy('id', 'asc')->get()); and take a screenshot, censoring sensitive info? It'd be good to see exactly what's being returned.

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.