查询返回数组的集合而不是模型的集合

时间:2017-04-04 22:37:07

标签: php laravel-5 laravel-5.3 php-7.1

我使用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": "test@gmail.com"
      +"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" => "test@gmail.com"
      2 => "test@gmail.com"
      "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问题吗?

1 个答案:

答案 0 :(得分:0)

它是一个PDO配置问题,我认为与此相关

配置/ 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