如何在laravel 5中返回数组而不是对象

时间:2017-07-27 05:51:55

标签: laravel-5 sql-server-2014

我在模特中使用过。

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

但在控制器中,我有这样的感觉。

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

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

但我想要这样

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

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

我已经尝试过使用get()和toArray(),但是它调用了未知的成员函数。 任何人都知道如何解决这个问题。

3 个答案:

答案 0 :(得分:3)

您只需使用laravel标准功能

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

更新:

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

输出将是:

更新:

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] => 
        )
)

我希望它会对你有用。

答案 1 :(得分:1)

toArray是Eloquent的模型方法,所以你需要一个Eloquent模型,试试这个:

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

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

答案 2 :(得分:0)

尝试一些这样的想法

$query = DB::connection()->getPdo()->query("select * from users");

$data = $query->fetchAll(\PDO::FETCH_ASSOC);

dd($data);

And for next row set

$array = [];

$query = DB::connection()->getPdo()->query("select * from users");

do {
    $data = $query->fetchAll(\PDO::FETCH_ASSOC);

    if ($data) {      
      array_push($array, $data);
    }
} while ($pdoData->nextRowset());