Raj
Raj

Reputation: 489

How to return array instead of object in laravel 5

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.

Upvotes: 8

Views: 16105

Answers (4)

Ajowi
Ajowi

Reputation: 505

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.

Upvotes: 0

Afzal Patel
Afzal Patel

Reputation: 144

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.

Upvotes: 8

Raghbendra Nayak
Raghbendra Nayak

Reputation: 1646

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.

Upvotes: 6

Arun pandian M
Arun pandian M

Reputation: 882

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

Upvotes: 2

Related Questions