查询结果不符合预期的地方
Where like query result not as expected
我已经编写了 eloquent 查询以在某些条件下获得搜索输入结果,但结果与预期不符
//i have 4 drivers registered by agent_id 12 in that 2 are pending registrations on searching name of the driver i should get only of those pending
public function pendingRegistrationSearch($agent_id, $search_input, $limit)
{
$pending_registration_search = Driver::where('agent_id' , $agent_id)
->where('registration_status', 'pending')
->orwhere('id', 'LIKE', '%'.$search_input.'%')
->orwhere('first_name', 'LIKE', '%'.$search_input.'%')
->orWhere('last_name', 'LIKE', '%'.$search_input.'%')
->orWhere('phone_number', 'LIKE', '%'.$search_input.'%')
->orWhere('registration_id', 'LIKE', '%'.$search_input.'%')
->select('id','first_name','last_name','phone_number','registration_status')
->orderBy('first_name', 'asc')
->limit(30)->offset($limit)
->get();
return Response::json([
'data' => $pending_registration_search
]);
}
//but i'm getting all the data based on search result, data have the drivers of all the agents too
{
"data": [
{
"id": 1,
"first_name": "Ajay",
"last_name": "singh",
"phone_number": "1234568790", data of agent_id 10
"registration_status": "pending"
},
{
"id": 2,
"first_name": "john",
"last_name": "machado",
"phone_number": "1234568790", data of agent_id 12
"registration_status": "processed"
}
]
}
我猜前两个条件不工作
谢谢
我认为问题出在这里:
where('agent_id' , $agent_id)
->where('registration_status', 'pending')
->orwhere('id', 'LIKE', '%'.$search_input.'%')
让我们用这个来理解:
( 'agent_id' = $agent_id ) && ( 'registration_status' = 'pending' )
|| ( condition ) || ( condition ) || ( condition ) || ( condition )
这种关系在这里行得通,但你需要:
( ( 'agent_id' = $agent_id ) && ( 'registration_status' = 'pending' ) )
|| (( condition ) || ( condition ) || ( condition ) || ( condition ) )
这种关系。所以我认为你可以在这里使用RAW query
。
我可能没有给你确切的解决方案,但问题可能是你没有应用某些条件。当您使用 orWhere
时,您通常应该将整个条件包装到闭包中,例如:
$pending_registration_search = Driver::where('agent_id' , $agent_id)
->where(function($q) use ($search_input) {
$q->where('registration_status', 'pending')
->orwhere('id', 'LIKE', '%'.$search_input.'%')
->orwhere('first_name', 'LIKE', '%'.$search_input.'%')
->orWhere('last_name', 'LIKE', '%'.$search_input.'%')
->orWhere('phone_number', 'LIKE', '%'.$search_input.'%')
->orWhere('registration_id', 'LIKE', '%'.$search_input.'%')
})->select('id','first_name','last_name','phone_number','registration_status')
->orderBy('first_name', 'asc')
->limit(30)->offset($limit)
->get();
例如上面的例子 agent_id
将始终使用条件,其他条件将使用 OR 创建,因此您现在将得到如下查询:
WHERE A AND (B OR C OR D)
在你的版本中,你在做什么:
WHERE A AND B OR C OR D
什么相同:
WHERE (A AND B) OR C OR D
所以如果 C 或 D 条件为真,则根本不使用 A 条件。
根据你写的,你也可以使用:
$pending_registration_search = Driver::where('agent_id' , $agent_id)
->where('registration_status', 'pending')
->where(function($q) use ($search_input) {
$q->where('id', 'LIKE', '%'.$search_input.'%')
->orwhere('first_name', 'LIKE', '%'.$search_input.'%')
->orWhere('last_name', 'LIKE', '%'.$search_input.'%')
->orWhere('phone_number', 'LIKE', '%'.$search_input.'%')
->orWhere('registration_id', 'LIKE', '%'.$search_input.'%')
})->select('id','first_name','last_name','phone_number','registration_status')
->orderBy('first_name', 'asc')
->limit(30)->offset($limit)
->get();
我已经编写了 eloquent 查询以在某些条件下获得搜索输入结果,但结果与预期不符
//i have 4 drivers registered by agent_id 12 in that 2 are pending registrations on searching name of the driver i should get only of those pending
public function pendingRegistrationSearch($agent_id, $search_input, $limit)
{
$pending_registration_search = Driver::where('agent_id' , $agent_id)
->where('registration_status', 'pending')
->orwhere('id', 'LIKE', '%'.$search_input.'%')
->orwhere('first_name', 'LIKE', '%'.$search_input.'%')
->orWhere('last_name', 'LIKE', '%'.$search_input.'%')
->orWhere('phone_number', 'LIKE', '%'.$search_input.'%')
->orWhere('registration_id', 'LIKE', '%'.$search_input.'%')
->select('id','first_name','last_name','phone_number','registration_status')
->orderBy('first_name', 'asc')
->limit(30)->offset($limit)
->get();
return Response::json([
'data' => $pending_registration_search
]);
}
//but i'm getting all the data based on search result, data have the drivers of all the agents too
{
"data": [
{
"id": 1,
"first_name": "Ajay",
"last_name": "singh",
"phone_number": "1234568790", data of agent_id 10
"registration_status": "pending"
},
{
"id": 2,
"first_name": "john",
"last_name": "machado",
"phone_number": "1234568790", data of agent_id 12
"registration_status": "processed"
}
]
}
我猜前两个条件不工作
谢谢
我认为问题出在这里:
where('agent_id' , $agent_id)
->where('registration_status', 'pending')
->orwhere('id', 'LIKE', '%'.$search_input.'%')
让我们用这个来理解:
( 'agent_id' = $agent_id ) && ( 'registration_status' = 'pending' )
|| ( condition ) || ( condition ) || ( condition ) || ( condition )
这种关系在这里行得通,但你需要:
( ( 'agent_id' = $agent_id ) && ( 'registration_status' = 'pending' ) )
|| (( condition ) || ( condition ) || ( condition ) || ( condition ) )
这种关系。所以我认为你可以在这里使用RAW query
。
我可能没有给你确切的解决方案,但问题可能是你没有应用某些条件。当您使用 orWhere
时,您通常应该将整个条件包装到闭包中,例如:
$pending_registration_search = Driver::where('agent_id' , $agent_id)
->where(function($q) use ($search_input) {
$q->where('registration_status', 'pending')
->orwhere('id', 'LIKE', '%'.$search_input.'%')
->orwhere('first_name', 'LIKE', '%'.$search_input.'%')
->orWhere('last_name', 'LIKE', '%'.$search_input.'%')
->orWhere('phone_number', 'LIKE', '%'.$search_input.'%')
->orWhere('registration_id', 'LIKE', '%'.$search_input.'%')
})->select('id','first_name','last_name','phone_number','registration_status')
->orderBy('first_name', 'asc')
->limit(30)->offset($limit)
->get();
例如上面的例子 agent_id
将始终使用条件,其他条件将使用 OR 创建,因此您现在将得到如下查询:
WHERE A AND (B OR C OR D)
在你的版本中,你在做什么:
WHERE A AND B OR C OR D
什么相同:
WHERE (A AND B) OR C OR D
所以如果 C 或 D 条件为真,则根本不使用 A 条件。
根据你写的,你也可以使用:
$pending_registration_search = Driver::where('agent_id' , $agent_id)
->where('registration_status', 'pending')
->where(function($q) use ($search_input) {
$q->where('id', 'LIKE', '%'.$search_input.'%')
->orwhere('first_name', 'LIKE', '%'.$search_input.'%')
->orWhere('last_name', 'LIKE', '%'.$search_input.'%')
->orWhere('phone_number', 'LIKE', '%'.$search_input.'%')
->orWhere('registration_id', 'LIKE', '%'.$search_input.'%')
})->select('id','first_name','last_name','phone_number','registration_status')
->orderBy('first_name', 'asc')
->limit(30)->offset($limit)
->get();