Php 在Laravel/Elount中返回多个而不是一个
我试图从mySQL数据库中获取数据,但由于某些原因,只有一个返回。以下是数据库中的表:Php 在Laravel/Elount中返回多个而不是一个,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我试图从mySQL数据库中获取数据,但由于某些原因,只有一个返回。以下是数据库中的表: 从位置选择* +----+------------+------------------+---------------------+---------------------+---------------+------------+ | id | account_id | name | created_at | updated_at | loca
从位置选择*代码>
+----+------------+------------------+---------------------+---------------------+---------------+------------+
| id | account_id | name | created_at | updated_at | location_code | deleted_at |
+----+------------+------------------+---------------------+---------------------+---------------+------------+
| 1 | 1 | Default Location | 2018-01-05 19:56:11 | 2018-01-05 19:56:11 | 86A8-6070 | NULL |
| 2 | 1 | Testing | 2018-01-07 15:15:16 | 2018-01-07 15:15:16 | BA5D-C4FD | NULL |
+----+------------+------------------+---------------------+---------------------+---------------+------------+
这是我的电话:
$locationSettings = Location::find($user->account_id);
\Log::info($locationSettings);
而且。。。以下是它的回报:
[2018-01-07 21:19:47] local.INFO: {"id":1,"account_id":1,"name":"Default Location","created_at":"2018-01-05 19:56:11","updated_at":"2018-01-05 19:56:11","location_code":"86A8-6070","deleted_at":null} [] []
为什么它不返回表中的两个条目,因为它们都有匹配的帐户id?试试看
$locationSettings=Location::where('account_id',$user->account_id)->get()代码>
默认情况下,find()
方法只返回一条记录。您需要一个集合,因此需要使用where子句使用get()
Try
$locationSettings=Location::where('account_id',$user->account_id)->get()代码>
默认情况下,find()
方法只返回一条记录。您需要一个集合,因此需要使用where子句get()
find only返回找到的第一个对象find only返回找到的第一个对象完美!因此Find
只返回第一个对象。这就更有意义了!当它可用时,最好的答案。find()
实际上默认情况下会查看id
列,因此当您说find(1)
时,它实际上会返回第一条记录,因为这是id=1的行。这太完美了!因此Find
只返回第一个对象。这就更有意义了!当它可用时,最好的答案。find()
实际上默认情况下会查看id
列,因此当您说find(1)
时,它实际上会返回第一条记录,因为这是id=1的行。