[英]how to use Laravel 8 query builder like eloquent for searching
I'm developing a simple CRM with Laravel 8 that needs to search in query string.我正在使用 Laravel 8 开发一个需要在查询字符串中搜索的简单 CRM。
My query string would be look like:我的查询字符串如下所示:
http://127.0.0.1:8000/admin/users/supervisor?q=john&gender=m
Now the problem is how can I search in controller?现在的问题是如何在 controller 中搜索?
I want to do like this:我想这样做:
public function index (Request $request)
{
$role = getRoleCode($request->role);
$roles = Role::where('role', '=', $role);
if ($request->q) {
$roles->where('name', 'like', "%$request->$q%");
}
if ($request->gender) {
$roles->where('gender', '=', $request->gender);
}
$role->orderBy('id', 'desc')->paginate(20);
return view('admin.users.index', [
'roles' => $roles,
'role_name' => config('settings.roles')[$role],
'role_en_name' => $request->role,
'q' => $request->q,
'gender' => $request->gender
]);
}
I wonder why its not working and what is the standard way to do this.我想知道为什么它不起作用以及执行此操作的标准方法是什么。
I've tried:我试过了:
Role::query();
but that didn't work either.但这也没有用。
I've also tried:我也试过:
$roles = Role::where('role', '=', $role)
->where('name', 'like', "%$request->$q%")
->where('gender', '=', $request->gender)
->orderBy('id', 'desc')
->paginate(20);
This codes works perfectly but we may not be sending the "q" or "gender" URL params.此代码完美运行,但我们可能不会发送“q”或“gender”URL 参数。
PS: Sorry for my bad English:) PS:对不起我的英语不好:)
If you want to conditionally add where
statements to your query you can use the if
statements like you are attempting to do or use the when
method of the Eloquent/Query Builder:如果您想有条件地将where
语句添加到查询中,您可以使用if
语句,就像您尝试做的那样,或者使用 Eloquent/Query Builder 的when
方法:
$roles = Role::where('role', $role)
->when($request->input('q'), fn ($query, $search) => $query->where('name', 'like', '%'. $search .'%'))
->when($request->input('gender'), fn ($query, $gender) => $query->where('gender', $gender))
->orderBy('id', 'DESC')
->paginate(20);
Laravel 8.x Docs - Queries - Conditional Clauses when
Laravel 8.x Docs - Queries - Conditional Clauses when
work with when() it's better与 when() 一起工作会更好
->when(true, function($query){$query->where('x', x);})
Your route should be like this:你的路线应该是这样的:
Route::get('/admin/users/{role}', [YourController::class, 'index']);
Your index function will be like this:您的索引 function 将是这样的:
public function index (Request $request,$role)
{
//old line $role = getRoleCode($request->role);
$role = getRoleCode($role);
$roles = Role::where('role', '=', $role);
if ($request->q) {
$roles->where('name', 'like', "%$request->$q%");
}
if ($request->gender) {
$roles->where('gender', '=', $request->gender);
}
$role->orderBy('id', 'desc')->paginate(20);
return view('admin.users.index', [
'roles' => $roles,
'role_name' => config('settings.roles')[$role],
'role_en_name' => $request->role,
'q' => $request->q,
'gender' => $request->gender
]);
}
There is a mistake Mr Mohamed $roles instead of $role Mohamed 先生有一个错误是 $roles 而不是 $role
//$roles->orderBy('id', 'desc')->paginate(20)... //$roles->orderBy('id', 'desc')->paginate(20)...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.