我有一个这样的 Laravel 查询但是我得到了这样的错误
Undefined variable: keyword
$bookings = DB::table('bookings')
->leftJoin('users', 'bookings.manager_id', '=', 'users.id')
->leftJoin('projects', 'bookings.pr_project_id', '=', 'projects.id')
->leftJoin('resources', 'bookings.pr_resource_id', '=', 'resources.id')
->Where('unavailable','=','0')
->orWhere(function ($query) {
$query->Where('users.name', 'LIKE', "%$keyword%")
->Where('projects.project_name', 'LIKE', "%$keyword%")
->Where('resources.r_name', 'LIKE', "%$keyword%");
})
->orderBy('bookings.pr_working_date', 'desc')
->paginate($perPage);
下面查询如何在Laravel中转换
select * from bookings left join users on bookings.manager_id=users.id left join projects on bookings.pr_project_id=projects.id left join resources on bookings.pr_resource_id=resources.id where unavailable='0' AND (users.name like '%One Web Series%' or projects.project_name like '%One Web Series%' or resources.r_name like '%One Web Series%')
最佳答案
您需要add the use()
像这样将变量传递到闭包范围:
function ($query) use($keyword) {
关于php - 如何使用 Laravel 构建 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48398519/