How to return array instead of object in laravel 5
Asked Answered
M

4

8

I have use in model .

$data = DB::select('select * from users');
return $data;

But in controller I have got like this.

Array
(
    [0] => stdClass Object
        (
            [Id] => 10
            [Name] => Sachin
            [Gender] => M
        )

    [1] => stdClass Object
        (
            [Id] => 12
            [Name] => Sourav
            [Gender] => M
        )
)

But I want Like this

Array
    (
        [0] => Array
            (
                [Id] => 10
                [Name] => Sachin
                [Gender] => M
            )

        [1] => Array
            (
                [Id] => 12
                [Name] => Sourav
                [Gender] => M
            )
    )

I already tried using get() and toArray() but its giving call to unknown member function. Anyone have an idea how to resolved this issue.

Memling answered 27/7, 2017 at 5:51 Comment(0)
N
6

You just use the laravel standard function

public function testData(){
    $data = YOUR_MODEL_NAME::all()->toArray();
    echo "<pre>";print_r($data);
}

UPDATED:

public function testData(){
    $data =  DB::select('select * from users');
    $data = collect($data)->map(function($x){ return (array) $x; })->toArray(); 
    echo "<pre>";print_r($data);
}

Output will be :

UPDATED:

Array
(
    [0] => Array
        (
            [email] => [email protected]
            [member_code] => BMC0001ADMIN
        )

    [1] => Array
        (
            [email] => [email protected]
            [member_code] => BMC0003ADMIN
        )

    [2] => Array
        (
            [email] => [email protected]
            [member_code] => BMC0002ADMIN
        )
)

Array
(
    [0] => Array
        (
            [id] => 1
            [drewry_user_id] => 1
            [email] => [email protected]
            [name] => BMC  Administrator
            [mobile] => 
            [member_name] => BMC-Admin
            [member_code] => BMC0001ADMIN
            [shipper_size] => Large
            [fk_role_id] => 1
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-12 04:56:31
            [updated_by] => 
            [deleted_at] => 
        )

    [1] => Array
        (
            [id] => 6
            [drewry_user_id] => 3
            [email] => [email protected]
            [name] => BMC  Analyst
            [mobile] => 
            [member_name] => BMC-Analyst
            [member_code] => BMC0003ADMIN
            [shipper_size] => Large
            [fk_role_id] => 3
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-26 10:55:41
            [updated_by] => 
            [deleted_at] => 
        )

    [2] => Array
        (
            [id] => 9
            [drewry_user_id] => 2
            [email] => [email protected]
            [name] => BMC Product Manager
            [mobile] => 
            [member_name] => BMC-Product-Manager
            [member_code] => BMC0002ADMIN
            [shipper_size] => Large
            [fk_role_id] => 2
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-14 09:09:10
            [updated_by] => 
            [deleted_at] => 
        )

    [3] => Array
        (
            [id] => 19
            [drewry_user_id] => 4
            [email] => [email protected]
            [name] => User 1  
            [mobile] => 
            [member_name] => User-1
            [member_code] => BMC0004CUSTOMER1
            [shipper_size] => Large
            [fk_role_id] => 4
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-27 11:31:20
            [updated_by] => 
            [deleted_at] => 
        )

    [4] => Array
        (
            [id] => 20
            [drewry_user_id] => 5
            [email] => [email protected]
            [name] => User 2  
            [mobile] => 
            [member_name] => User-2
            [member_code] => BMC0004CUSTOMER2
            [shipper_size] => Large
            [fk_role_id] => 4
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-18 06:34:08
            [updated_by] => 
            [deleted_at] => 
        )

    [5] => Array
        (
            [id] => 21
            [drewry_user_id] => 6
            [email] => [email protected]
            [name] => User 3  
            [mobile] => 
            [member_name] => User-3
            [member_code] => BMC0004CUSTOMER3
            [shipper_size] => Large
            [fk_role_id] => 4
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-18 06:35:25
            [updated_by] => 
            [deleted_at] => 
        )

    [6] => Array
        (
            [id] => 22
            [drewry_user_id] => 7
            [email] => [email protected]
            [name] => User 4  
            [mobile] => 
            [member_name] => User-4
            [member_code] => BMC0004CUSTOMER4
            [shipper_size] => Large
            [fk_role_id] => 4
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-18 06:35:54
            [updated_by] => 
            [deleted_at] => 
        )

    [7] => Array
        (
            [id] => 23
            [drewry_user_id] => 8
            [email] => [email protected]
            [name] => User 5  
            [mobile] => 
            [member_name] => User-5
            [member_code] => BMC0004CUSTOMER5
            [shipper_size] => Large
            [fk_role_id] => 4
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-25 15:35:46
            [updated_by] => 
            [deleted_at] => 
        )
)

I hope it will work to you.

Nahuatl answered 27/7, 2017 at 6:8 Comment(6)
I know its works with eloquent but I could not use Eloquent in my situation.so please do answer if you know how to do with DB::selectMemling
So you want it without of Eloquent ?Nahuatl
You will pass raw query always ?Nahuatl
yes I have row query. like DB::select('my query')Memling
Please check my updated answer, is that work for you?Nahuatl
Let us continue this discussion in chat.Nahuatl
E
8

If you just need to get raw nested arrays of data, without using Eloquent:

$data = DB::connection()->getPdo()
    ->query("SELECT * FROM users")
    ->fetchAll(\PDO::FETCH_ASSOC);

This should be much more efficient than the current accepted answer.

Equip answered 18/4, 2019 at 7:48 Comment(3)
Accepted answer will build models and on large datasets those approaches will crash. This answer is better because on large datasets you want to work with more raw data.Leakage
performance-wise this is the only correct answer. The other answers all convert into collections first and then back into arrays. I know the OP didn't ask for the best performing solution but if you're looking to get array data instead of Eloquent data I imagine it's probably for optimization reasons, and this is a great way to do it.Detritus
however this answer will not log the queryWreathe
N
6

You just use the laravel standard function

public function testData(){
    $data = YOUR_MODEL_NAME::all()->toArray();
    echo "<pre>";print_r($data);
}

UPDATED:

public function testData(){
    $data =  DB::select('select * from users');
    $data = collect($data)->map(function($x){ return (array) $x; })->toArray(); 
    echo "<pre>";print_r($data);
}

Output will be :

UPDATED:

Array
(
    [0] => Array
        (
            [email] => [email protected]
            [member_code] => BMC0001ADMIN
        )

    [1] => Array
        (
            [email] => [email protected]
            [member_code] => BMC0003ADMIN
        )

    [2] => Array
        (
            [email] => [email protected]
            [member_code] => BMC0002ADMIN
        )
)

Array
(
    [0] => Array
        (
            [id] => 1
            [drewry_user_id] => 1
            [email] => [email protected]
            [name] => BMC  Administrator
            [mobile] => 
            [member_name] => BMC-Admin
            [member_code] => BMC0001ADMIN
            [shipper_size] => Large
            [fk_role_id] => 1
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-12 04:56:31
            [updated_by] => 
            [deleted_at] => 
        )

    [1] => Array
        (
            [id] => 6
            [drewry_user_id] => 3
            [email] => [email protected]
            [name] => BMC  Analyst
            [mobile] => 
            [member_name] => BMC-Analyst
            [member_code] => BMC0003ADMIN
            [shipper_size] => Large
            [fk_role_id] => 3
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-26 10:55:41
            [updated_by] => 
            [deleted_at] => 
        )

    [2] => Array
        (
            [id] => 9
            [drewry_user_id] => 2
            [email] => [email protected]
            [name] => BMC Product Manager
            [mobile] => 
            [member_name] => BMC-Product-Manager
            [member_code] => BMC0002ADMIN
            [shipper_size] => Large
            [fk_role_id] => 2
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-14 09:09:10
            [updated_by] => 
            [deleted_at] => 
        )

    [3] => Array
        (
            [id] => 19
            [drewry_user_id] => 4
            [email] => [email protected]
            [name] => User 1  
            [mobile] => 
            [member_name] => User-1
            [member_code] => BMC0004CUSTOMER1
            [shipper_size] => Large
            [fk_role_id] => 4
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-27 11:31:20
            [updated_by] => 
            [deleted_at] => 
        )

    [4] => Array
        (
            [id] => 20
            [drewry_user_id] => 5
            [email] => [email protected]
            [name] => User 2  
            [mobile] => 
            [member_name] => User-2
            [member_code] => BMC0004CUSTOMER2
            [shipper_size] => Large
            [fk_role_id] => 4
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-18 06:34:08
            [updated_by] => 
            [deleted_at] => 
        )

    [5] => Array
        (
            [id] => 21
            [drewry_user_id] => 6
            [email] => [email protected]
            [name] => User 3  
            [mobile] => 
            [member_name] => User-3
            [member_code] => BMC0004CUSTOMER3
            [shipper_size] => Large
            [fk_role_id] => 4
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-18 06:35:25
            [updated_by] => 
            [deleted_at] => 
        )

    [6] => Array
        (
            [id] => 22
            [drewry_user_id] => 7
            [email] => [email protected]
            [name] => User 4  
            [mobile] => 
            [member_name] => User-4
            [member_code] => BMC0004CUSTOMER4
            [shipper_size] => Large
            [fk_role_id] => 4
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-18 06:35:54
            [updated_by] => 
            [deleted_at] => 
        )

    [7] => Array
        (
            [id] => 23
            [drewry_user_id] => 8
            [email] => [email protected]
            [name] => User 5  
            [mobile] => 
            [member_name] => User-5
            [member_code] => BMC0004CUSTOMER5
            [shipper_size] => Large
            [fk_role_id] => 4
            [fk_country_id] => 
            [timezone] => Asia/Kolkata
            [status] => 1
            [created_at] => 
            [created_by] => 
            [updated_at] => 2017-07-25 15:35:46
            [updated_by] => 
            [deleted_at] => 
        )
)

I hope it will work to you.

Nahuatl answered 27/7, 2017 at 6:8 Comment(6)
I know its works with eloquent but I could not use Eloquent in my situation.so please do answer if you know how to do with DB::selectMemling
So you want it without of Eloquent ?Nahuatl
You will pass raw query always ?Nahuatl
yes I have row query. like DB::select('my query')Memling
Please check my updated answer, is that work for you?Nahuatl
Let us continue this discussion in chat.Nahuatl
S
2

toArray is a model method of Eloquent, so you need to a Eloquent model, try this:

 User::all()->toArray();

http://laravel.com/docs/eloquent#collections

Scaffolding answered 27/7, 2017 at 5:58 Comment(2)
This only returns an array of models. I guess what Raj wants is an array of array. toArray() only changes collection to array.Dorton
This converts the data to a collection and then back to an array. Huge performance loss!Coldhearted
B
0

After several trials of the solutions suggested, the problem why Laravel complains that the returned set is db:query (and thereby not eloquent collection - am new to these terms) is that the given line of code is incomplete:

$data =  DB::select('select * from users');
//should change to something like
$data =  DB::select('select * from users')->get();
//then to get pure array, just add the to-array method as:
$data =  DB::select('select * from users')->get()->toArray();
//if the above does not work, then as a last resort (from answer here) 
$data = collect($data)->map(function($x){ return (array) $x; })->toArray();

I hope this helps someone in same situation. Tested at Laravel 11.x.

Bear answered 21/7 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.