我写了一些雄辩的查询来获取某些条件的搜索输入结果,但结果并不像预期的那样
//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"
}
]
}
我认为条件不起作用的前两个
谢谢
答案 0 :(得分:2)
我不能给你一些确切的解决方案,但问题可能是你没有适用某些条件。当你使用 $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
例如,在上面WHERE A AND (B OR C OR D)
条件中将始终使用,其他条件将使用OR创建,因此您现在将获得如下查询:
WHERE A AND B OR C OR D
在您所处位置的版本中:
WHERE (A AND B) OR C OR D
与...相同:
$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();
因此,如果C或D条件为真,则根本不使用条件。
基于你写的,你也可以使用:
interface String {
padStart(targetLength: number, padString?: string): string;
}
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
// https://developer.mozilla.org/enUS/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart(targetLength, padString) {
targetLength = targetLength >> 0; //floor if number or convert non-number to 0;
padString = String(padString || ' ');
if (this.length > targetLength) {
return String(this);
}
else {
targetLength = targetLength-this.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed
}
return padString.slice(0, targetLength) + String(this);
}
};
}
答案 1 :(得分:1)
我认为问题在于:
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
。