【发布时间】:2021-11-24 23:18:23
【问题描述】:
我想在初始查询中按子查询的结果过滤我的查询 当我运行上面的代码时,我得到一个错误,说找不到打印的列
$data = Order::
join('clients', 'orders.client_id', '=', 'clients.id')
->join('addresses', 'orders.address_id', '=', 'addresses.id')
->join('br_codes', 'br_codes.id', '=', 'orders.br_code_id')
->join('users', 'orders.user_id', '=', 'users.id')
->join('cities', 'addresses.city_id', '=', 'cities.id')
->select([
'orders.id',
'orders.id as OrderID',
'clients.name as clientname',
'br_codes.br_code',
'addresses.address',
'cities.name as cityname',
'addresses.phone',
'addresses.zip_code',
'users.name as username',
'orders.created_at',
'orders.status',
'cities.tap',
'orders.paid',
'orders.recover',
'orders.date_postponed as reported',
'orders.status_shipper as shipper',
DB::raw('(select count(*) from notes where notes.order_id = orders.id) cnotes'),
DB::raw('(select count(*) from addresses ad2 JOIN clients cl on cl.id=ad2.client_id where addresses.phone = ad2.phone AND cl.user_id='.Auth::id().') dupuser'),
DB::raw('(select sum(items.quantity*items.price) from items where items.order_id = orders.id) price'),
DB::raw('(select count(*) from notes where notes.order_id = orders.id and notes.note like "%Invoice printed%" and notes.user_id='.Auth::id().') printed'),
'orders.pin',
DB::raw('(select users.name from users join affect_orders on affect_orders.user_id=users.id where affect_orders.order_id=orders.id limit 1) as shipper')
])->whereIn('orders.status',[1,4]);
if($request->printed){
$data->where('printed','>',1);
}else{
$data->where('printed',0);
}
【问题讨论】:
-
唯一的方法是将整个查询包装在另一个具有该条件的查询中,就像编写普通 SQL 查询时一样。如果您想使用联接而不是关系,那么使用 eloquent 也没有意义。
标签: mysql laravel laravel-query-builder