laravel-5sql-server-2014

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.


Solution

  • 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] => bmc-admin@gmail.com
                [member_code] => BMC0001ADMIN
            )
    
        [1] => Array
            (
                [email] => bmc-analyst@gmail.com
                [member_code] => BMC0003ADMIN
            )
    
        [2] => Array
            (
                [email] => bmc-product-manager@gmail.com
                [member_code] => BMC0002ADMIN
            )
    )
    

    Array
    (
        [0] => Array
            (
                [id] => 1
                [drewry_user_id] => 1
                [email] => bmc-admin@gmail.com
                [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] => bmc-analyst@gmail.com
                [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] => bmc-product-manager@gmail.com
                [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] => bmc-customer1@gmail.com
                [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] => bmc-customer2@gmail.com
                [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] => bmc-customer3@gmail.com
                [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] => bmc-customer4@gmail.com
                [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] => bmc-customer5@gmail.com
                [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.