2017-07-27 67 views
1

我在模型中使用。如何在laravel中返回數組而不是對象5

$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()和指定者(),但它給予調用未知的成員函數嘗試。 任何人都有一個想法如何解決這個問題。

回答

1

你只需要使用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] => [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] => 
     ) 
) 

我希望它會工作給你。

+0

我知道它的作品有雄辯,但我不能在我的情況下使用雄辯,所以請做回答,如果你知道如何處理DB :: select – Raj

+0

所以你想它沒有雄辯嗎? –

+0

是沒有雄辯如何可能 – Raj

相關問題