2017-04-04 110 views
0

我使用Laravel 5.3.31和PHP 7.1.2創建了一個包含家園的網站。 我有一個遠程服務器託管的同樣確切的站點,也使用Laravel 5.3.31和PHP 7.1.3。查詢返回數組而不是集合模型的集合

我有我的觀點一個下面的代碼:

$users = \DB::table('users')->get(); 
dd($users); 

在宅基地網站,我得到以下結果:

Collection {#327 ▼ 
    #items: array:1 [▼ 
    0 => {#334 ▼ 
     +"uid": 1 
     +"user_name": "test" 
     +"email": "[email protected]" 
     +"password": "somehashcode" 
     +"status": "active" 
     +"remember_token": "priEYh5ewdq2IlwCwOyNc8x1w0L1zNdWHV1wLznaBfhxoWex4rDvb8lijc5T" 
     +"created_at": "2017-03-20 18:30:46" 
     +"updated_at": "2017-03-28 17:12:49" 
    } 
    ] 
} 

在我的遠程服務器上,另一方面,我得到這樣的結果,而不是:

Collection {#320 ▼ 
    #items: array:1 [▼ 
    0 => array:16 [▼ 
     "uid" => 1 
     0 => 1 
     "user_name" => "test" 
     1 => "test" 
     "email" => "[email protected]" 
     2 => "[email protected]" 
     "password" => "somehashcode" 
     3 => "somehashcode" 
     "status" => "active" 
     4 => "active" 
     "remember_token" => "WnKr77qKbgRwekZEdc9DhjLncYre5h7WvZrW7OkpOG8FZJuNFxC0CqRA5lEK" 
     5 => "WnKr77qKbgRwekZEdc9DhjLncYre5h7WvZrW7OkpOG8FZJuNFxC0CqRA5lEK" 
     "created_at" => null 
    6 => null 
    "updated_at" => "2017-04-03 19:49:50" 
    7 => "2017-04-03 19:49:50" 
] 

] }

因此,我無法在遠程服務器上執行$ user-> uid,我無法理解可能是什麼原因造成的。有任何想法嗎?這可能是一個PHP問題?

+0

在另一塊電路板上發現的解決方案。問題出在config/database.php文件中。它錯過了'fetch'這一行=> PDO :: FETCH_OBJ。 https://laracasts.com/discuss/channels/laravel/query-returning-collection-of-arrays-instead-of-collection-of-models – user3018580

+0

你爲什麼不自己回答問題,因爲你找到了解決方案? –

回答

0

它的一個PDO配置問題,我認爲與此相關的

的config/database.php中

/* 
|-------------------------------------------------------------------------- 
| PDO Fetch Style 
|-------------------------------------------------------------------------- 
| 
| By default, database results will be returned as instances of the PHP 
| stdClass object; however, you may desire to retrieve records in an 
| array format for simplicity. Here you can tweak the fetch style. 
| 
*/ 

'fetch' => PDO::FETCH_OBJ, 

我database.php中文件缺少這一行。一旦我添加它,一切都很好。

圖片來源:https://laracasts.com/discuss/channels/laravel/query-returning-collection-of-arrays-instead-of-collection-of-models