2017-04-04 131 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