2017-05-07 155 views
0

iam正在为网站做一个搜索条件,我面对的问题是,如果在数据库中我有更多的驱动程序的同名,我的查询只返回最后一个它记录发现, 首先IAM从用户获取输入,并在表中调用驱动程序搜索,含有相同的,因为他进入输入,代码记录:后我的laravel查询返回的只是数据库中的最后一条记录

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; 
} 

因此iam使用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函数输出($事故):

[[{ “ID”:1, “accidentFloor”: “”, “accidentRecord”: “rexord试验”, “地址”:“哈雷特hreik “,”location“:”no location“,”region“:”“,”colleague“:”“,”policeMan“:”“,”roadDirection“:”“,”roadType“:”“,”roadNumbers“: “”, “roadWidth”: “”, “日期”: “2017年4月12日”, “时间”: “16时28分29秒”, “愿景”: “”, “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”,“region”:“”,“colleague”:“”,policeman“:”“,”roadDirection“:”“,”roadType“: “”,“R oadNumbers “:””, “roadWidth”: “”, “日期”: “2017年4月12日”, “时间”: “16时28分29秒”, “愿景”: “”, “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测试“,”地址“:”haret hreik“,”location“:”no location“,”region“:”“,”colleague“:”“,”policeMan“:”“,”roadDirection“:”“, roadType “:””, “roadNumbers”: “”, “roadWidth”: “”, “日期”: “2017年4月12日”, “时间”: “16时28分29秒”, “愿景”: “” “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" , “区域”: “”, “同事”: “”, “警察”: “”, “roadDirection”: “”, “roadType”: “\ u0645 \ u0646 \ u0639 \ u0637 \ u0641”,“roadNumbers “:” 2" , “roadWidth”: “”, “日期”: “2017年5月1日”, “时间”: “13点21分26秒”, “愿景”: “”, “weatherCondition”: “” ,“expertId”:1,“accidentGenerationNumber”:“2017-05-0113:21:261”,“completedDate”:“”,“freeDescription”:“”,“created_at”:“2017-05-01 13:21 :26" , “的updated_at”: “2017年5月1日13时21分26秒”, “deleted_at”:空}]]

控制台输出:

(4)[阵列( 1),阵列(1),阵列(1),阵列(1)]
: Array(1)
: 阵列(1)
: 阵列(1)
: 阵列(1)
长度 : : 阵列(0)

回答

1

Your for loop

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

已经结束写同一个$accident变量。因此你只能在变量中获得最后一条记录。
更改,如果条件和循环,这样的:

$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包含所有事故的数据。

+0

多数民众赞成那是正确的,我试图添加$事故[$ x],所以它可能会将它们添加到数组,但仍然没有工作 – mahditrad

+1

试试我上面写的代码。 – gaganshera

+0

好吧,这有点帮助,我现在得到我的表3行,这是在我的数据库中包含该名称的行数相同,但作为第二个问题,表是空的,因为我在控制台中看到,它返回3个数组,但是为空 – mahditrad

相关问题