使用Laravel 5.4我试图运行这个查询,让我在phpMyAdmin上得到结果:
select * from `orders`
inner join `users` on `user_id` = `users`.`id`
where `status` = ?
and (`orders`.`id` LIKE ? or `first_name` LIKE ? or `last_name` LIKE ? or `company` LIKE ?
问题是,虽然我在phpMyAdmin中得到了结果,但我在代码中得到一个空结果。
我尝试了两种方法:
$orders = Order::join('users', 'user_id', '=', 'users.id')
->whereRaw("status = ? and (orders.id LIKE '%?%' or first_name LIKE '%?%' or last_name LIKE '%?%' or company LIKE '%?%')",
[$status, $search, $search, $search, $search])->get();
和
$orders = Order::join('users', 'user_id', '=', 'users.id')
->whereRaw("status = :status and
(orders.id LIKE :search_id or first_name LIKE :search_first or last_name LIKE :search_last or company LIKE :search_company)",
['status'=>$status, 'search_id'=>$search, 'search_first'=>$search, 'search_last'=>$search, 'search_company'=>$search])
->get();
我做错了什么?
答案 0 :(得分:0)
我这样解决了这个问题:
$search = '%' . $request->input('search.value') . '%';
$orders = Order::join('users', 'users.id', '=', 'orders.user_id')
->whereRaw("status = ? and (orders.id LIKE ? or first_name LIKE ? or last_name LIKE ? or company LIKE ?)",[$status, $search, $search, $search, $search])