问题描述
我遇到了未定义的偏移问题。这是我的控制器:
if($request->submit == "online")
{
if(!empty($_POST['speciality'])){
$users = User::where('speciality','=',$_POST['speciality'])->orWhere('speciality1',$_POST['speciality'])
->orWhere('speciality2',$_POST['speciality'])->get();
return view('result',compact('users'));
}
}
我正在使用 Laravel 6。 这是我的刀片:
@foreach($users)
hello {{$user['full_name']}}
@endforeach
我需要这方面的帮助。谢谢!
解决方法
在这种情况下,您实际上不能使用 orWhere,因为您已经通过调用 get() 方法执行了查询,之后结果将是一个集合而不是查询构建器。
解决方案应该只是删除第一个 get 方法调用。
,您在集合上使用 orWhere(即在调用 ->get() 之后)。你不能那样做。您需要在模型上使用它(好吧,查询构建器)。
所以你的代码应该是这样的:
if($request->submit == "online")
{
if(!empty($_POST['speciality'])){
$onlineUsers = User:: where('online_position','=',1);
$users = $onlineUsers>where('speciality',$_POST['speciality'])
->orWhere('speciality1',$_POST['speciality'])
->orWhere('speciality2',$_POST['speciality'])->get();
return view('result',compact('users'));
}
}