我想在 laravel 中实现正确的查询,如果有人可以帮助我有此查询SELECT * FROM `users` WHERE ((`is_verified` = 1) AND (`first_name` like '%%' or `middle_name` like '%%' or `last_name` like '%%' or `email` like '%%'))我的代码中有这个$user->where([ ['is_verified', '=', 1] ]) ->where('first_name', 'like', "%$search%") ->orWhere('middle_name', 'like', "%$search%") ->orWhere('last_name', 'like', "%$search%") ->orWhere('email', 'like', "%$search%");但它产生SELECT * FROM `users` WHERE ((`is_verified` = '1') and `first_name` like '%%' or `middle_name` like '%%' or `last_name` like '%%' or `email` like '%%')
2 回答
翻阅古今
TA贡献1780条经验 获得超5个赞
您必须在“closure-where”子句中使用匿名函数。
$user->where([ ['is_verified', '=', 1] ])->where(function ($query) use ($search) { $query->where('first_name', 'like', "%$search%") ->orWhere('middle_name', 'like', "%$search%") ->orWhere('last_name', 'like', "%$search%") ->orWhere('email', 'like', "%$search%"); });
千巷猫影
TA贡献1829条经验 获得超7个赞
您必须使用匿名函数,如下代码所示:
User::where(function ($query){ $query->where('is_verified', 1); })->where(function ($query) use ($search) { $query->where('first_name', 'like', "%$search%") ->orWhere('middle_name', 'like', "%$search%") ->orWhere('middle_name', 'like', "%$search%") ->orWhere('email', 'like', "%$search%"); })->toSql();
- 2 回答
- 0 关注
- 74 浏览
添加回答
举报
0/150
提交
取消