Laravel whereRaw有几个参数

时间:2018-03-01 14:21:46

标签: laravel-5 laravel-query-builder

使用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();

我做错了什么?

1 个答案:

答案 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])