【问题标题】:my laravel query is returning only the last record in the database我的 laravel 查询只返回数据库中的最后一条记录
【发布时间】:2017-10-05 12:07:21
【问题描述】:

我正在为一个网站做一个搜索条件,我面临的问题是,如果在数据库中我有多个同名驱动程序,我的查询只返回它找到的最后一条记录, 首先,我从用户那里获取输入,然后在一个名为 drivers 的表中搜索包含与他输入的输入相同的记录,代码:

if(!empty($name)||!empty($policynum)||!empty($platenum)||!empty($platecode)) // ** if one of name or policy# or platenum or platecode  is set ** //
{
$query = DB::table('drivers');
if(!empty($name))
$query -> where('driverName',$name);
if(!empty($policynum))
$query -> where('policyNumber',$policynum);
if(!empty($platenum))
$query -> where('plateNumber',$platenum);
if(!empty($platecode))
$query -> where('plateCode',$platecode);

$result = $query->get();
 $result=json_encode($result,true);
 $result=json_decode($result,true);
 }                                // --- end of if 4 inputs set --- //

在我得到司机之后,我在司机数据库中有一个名为事故id的属性,所以现在我必须转到事故数据库表,并搜索id与事故id相同的事故,代码:

if(count($result)>0 && empty($expname))   // if result array returns >0 results and exp-name empty //

{
for($x=0;$x<count($result);$x++)
$accident= accident::where('id',$result[$x]['accidentId'])
->get(); 
return $accident; 
}

所以我使用 for 循环遍历我从驱动程序表中获得的所有结果,以查看驱动程序表中的任何结果是否在事故表中具有相同的 id,如果发现我想获取所有事故行,并且返回它,我把它放在一个表格中,如下所示:

 <tr ng-repeat="accident in accidents" > <!-- -->
                <td>{{accident.accidentRecord}}</td>
                <td>{{accident.expertId}}</td>
                <td>{{accident.address}}</td>
                <td>{{accident.date}} --  -- {{accident.time}}</td>
                <td>{{accident.completedDate}}</td>

如果需要的话还有意外的js代码:

$scope.search=function(accident)
{
  $http({
      method:'GET',
      url:'getAccident',
      params: {accident:accident}
  })
      .success(function(data){
          $scope.accidents=data;
          console.log(data);
      })
      .error(function(err){
          console.log(err);
      })
};

但该表仅显示我的数据库中具有相同 id 的最后一行,而不是所有行,有人可以帮忙吗?

php 函数输出($accident):

[[{"id":1,"accidentFloor":"","accidentRecord":"rexord test","address":"haret hreik","location":"no location","re​​gion": "","同事":"","policeMan":"","roadDirection":"","roadType":"","roadNumbers":"","roadWidth":"","date":" 2017-04-12","time":"16:28:29","vision":"","weatherCondition":"","expertId":1,"accidentGenerationNumber":"2017-04-1216: 28:291","completedDate":"","freeDescription":"","created_at":"2017-04-12 16:28:29","updated_at":"2017-04-12 16:28: 29","deleted_at":null}],[{"id":1,"accidentFloor":"","accidentRecord":"rexord test","address":"haret hreik","location":"no location","re​​gion":"","colleague":"","policeMan":"","roadDirection":"","roadType":"","roadNumbers":"","roadWidth":" ","date":"2017-04-12","time":"16:28:29","vision":"","weatherCondition":"","expertId":1,"accidentGenerationNumber": "2017-04-1216:28:291","completedDate":"","freeDescription":"","created_at":"2017-04-12 16:28:29","updated_at":"2017- 04-12 16:28:29","deleted_at":null}],[{"id":1,"accidentFloor":"","accidentR ecord":"rexord test","address":"haret hreik","location":"no location","re​​gion":"","colleague":"","policeMan":"","roadDirection" :"","roadType":"","roadNumbers":"","roadWidth":"","date":"2017-04-12","time":"16:28:29"," vision":"","weatherCondition":"","expertId":1,"accidentGenerationNumber":"2017-04-1216:28:291","completedDate":"","freeDescription":""," created_at":"2017-04-12 16:28:29","updated_at":"2017-04-12 16:28:29","deleted_at":null}],[{"id":3," accidentFloor":"","accidentRecord":"bir al abed","address":"borj","location":"33.8591245,35.5110078","re​​gion":"","colleague":"","policeMan ":"","roadDirection":"","roadType":"\u0645\u0646\u0639\u0637\u0641","roadNumbers":"2","roadWidth":"","date":"2017 -05-01","time":"13:21:26","vision":"","weatherCondition":"","expertId":1,"accidentGenerationNumber":"2017-05-0113:21 :261","completedDate":"","freeDescription":"","created_at":"2017-05-01 13:21:26","updated_at":"2017-05-01 13:21:26 ","deleted_at":null}]]

控制台输出:

(4) [数组(1), 数组(1), 数组(1), 数组(1)]
0 : 数组(1)
1 : 数组(1)
2 : 数组(1)
3 : 数组(1)
长度 : 4
原型 : 数组(0)

【问题讨论】:

    标签: php mysql angularjs laravel


    【解决方案1】:

    你的 for 循环

    for($x=0;$x<count($result);$x++)
    $accident= accident::where('id',$result[$x]['accidentId'])
    ->get(); 
    

    一遍又一遍地重写同一个$accident变量。因此,您始终只能在变量中获得最后一条记录。
    将 if 条件和循环更改为:

    $accidents = [];
    if(count($result)>0 && empty($expname))   // if result array returns >0 results and exp-name empty //
    {
        for($x=0;$x<count($result);$x++)
            $accidents[] = accident::where('id',$result[$x]['accidentId'])->get(); 
        return $accidents; 
    }
    

    $accidents 包含所有事故数据。

    【讨论】:

    • 没错,我尝试添加 $accident[$x] ,所以它可能会将它们添加到数组中,但仍然没有工作
    • 好的,这在某种程度上很有帮助,我现在在我的表中获得 3 行,这与我的数据库中包含该名称的行数相同,但作为第二个问题,表是空的,正如我在控制台中看到的,它返回 3 个数组但为空
    • 只需对$accidents 变量执行print_r 并查看它显示的内容。
    • 它显示了我想要的 3 行,所以查询有效,但为什么我在表中得到空行? ,我张贴了填充表格的html部分,你认为我必须编辑其中的任何内容吗?
    • 我不确定您最终是如何编写 php 代码的,但它为每个表行创建了一个额外的数组。尝试将角度代码更改为accident.0.accidentRecordaccident[0]accidentRecord,我不太确定。
    猜你喜欢
    • 2018-02-05
    • 1970-01-01
    • 2021-01-31
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多