我正在获取角色等于 teacher ,状态为 active 和其他 orWhere 的用户数据。这个集合也给了我超级管理员用户,但为什么呢?
I am fetching data of user where role equals to teacher , status is active and other orWhere. This collection gives me super admin user also but why?
$data['tutors'] = User::where('status', 'active')
->whereRelation('role','name', 'teacher')
->where('name', 'like', "%" . $req . "%")
->where('first_name', 'like', "%" . $req . "%")
->orWhere('last_name', 'like', "%" . $req . "%")
->orWhere('description', 'like', "%" . $req . "%")
->orWhereRelation('country','name', 'like', "%" . $req . "%")
->orWhereRelation('state','name', 'like', "%" . $req . "%")
->orWhereRelation('city','name', 'like', "%" . $req . "%")
->orWhereRelation('languages.language','name', 'like', "%" . $req . "%")
->orWhereRelation('gigs','title', 'like', "%" . $req . "%")
->orWhereRelation('gigs','price', 'like', "%" . $req . "%")
->orWhereRelation('gigs','description', 'like', "%" . $req . "%")
->orWhereRelation('skills.skill','name', 'like', "%" . $req . "%")
->with('languages.language')
->with('skills.skill')
->with('country')->paginate(5);
我正在研究搜索。我面临查询问题。什么时候我搜索超级。它也给了我超级管理员用户,但我正在使用 ->whereRelation('role','name', 'teacher')
因为如果任何 orWhere 为真,记录将被提取,你应该将你的 orWhere 分组在一个语句中:
$data['tutors'] = User::where('status', 'active')
->whereRelation('role','name', 'teacher')
->where(function ($query) use ($req) {
$query->where('name', 'like', "%" . $req . "%")
->orWhere('first_name', 'like', "%" . $req . "%")
->orWhere('last_name', 'like', "%" . $req . "%")
->orWhere('description', 'like', "%" . $req . "%")
->orWhereRelation('country','name', 'like', "%" . $req . "%")
->orWhereRelation('state','name', 'like', "%" . $req . "%")
->orWhereRelation('city','name', 'like', "%" . $req . "%")
->orWhereRelation('languages.language','name', 'like', "%" . $req . "%")
->orWhereRelation('gigs','title', 'like', "%" . $req . "%")
->orWhereRelation('gigs','price', 'like', "%" . $req . "%")
->orWhereRelation('gigs','description', 'like', "%" . $req . "%")
->orWhereRelation('skills.skill','name', 'like', "%" . $req . "%");
})
->with('languages.language')
->with('skills.skill')
->with('country')->paginate(5);
$data['tutors'] = User::where('status', 'active')
->whereRelation('role','name', 'teacher')
->where('name', 'like', "%" . $req . "%")
->where('first_name', 'like', "%" . $req . "%")
->orWhere('last_name', 'like', "%" . $req . "%")
->orWhere('description', 'like', "%" . $req . "%")
->orWhereRelation('country','name', 'like', "%" . $req . "%")
->orWhereRelation('state','name', 'like', "%" . $req . "%")
->orWhereRelation('city','name', 'like', "%" . $req . "%")
->orWhereRelation('languages.language','name', 'like', "%" . $req . "%")
->orWhereRelation('gigs','title', 'like', "%" . $req . "%")
->orWhereRelation('gigs','price', 'like', "%" . $req . "%")
->orWhereRelation('gigs','description', 'like', "%" . $req . "%")
->orWhereRelation('skills.skill','name', 'like', "%" . $req . "%")
->with('languages.language')
->with('skills.skill')
->with('country')->paginate(5);
我正在研究搜索。我面临查询问题。什么时候我搜索超级。它也给了我超级管理员用户,但我正在使用 ->whereRelation('role','name', 'teacher')
因为如果任何 orWhere 为真,记录将被提取,你应该将你的 orWhere 分组在一个语句中:
$data['tutors'] = User::where('status', 'active')
->whereRelation('role','name', 'teacher')
->where(function ($query) use ($req) {
$query->where('name', 'like', "%" . $req . "%")
->orWhere('first_name', 'like', "%" . $req . "%")
->orWhere('last_name', 'like', "%" . $req . "%")
->orWhere('description', 'like', "%" . $req . "%")
->orWhereRelation('country','name', 'like', "%" . $req . "%")
->orWhereRelation('state','name', 'like', "%" . $req . "%")
->orWhereRelation('city','name', 'like', "%" . $req . "%")
->orWhereRelation('languages.language','name', 'like', "%" . $req . "%")
->orWhereRelation('gigs','title', 'like', "%" . $req . "%")
->orWhereRelation('gigs','price', 'like', "%" . $req . "%")
->orWhereRelation('gigs','description', 'like', "%" . $req . "%")
->orWhereRelation('skills.skill','name', 'like', "%" . $req . "%");
})
->with('languages.language')
->with('skills.skill')
->with('country')->paginate(5);