关于laravel多个条件的关联查询问题 :
表 order 订单表:
id自增IDorder_id订单号paid_date支付时间
表 order_product 订单产品表:
id自增IDfk_order_id订单号,外键product_name名称product_number编号quantity数量
表关系:
order - 1:n - order_product
需求:
通过 Laravel Eloquent ORM 实现以下原生 SQL:
select * from order as A inner join order_product as B on A.order_id=B.fk_order_id where (A.paid_date between '2016-01-01' and '2016-09-01') and B.product_name like '%Apple iPhone%'
手册看了几次,尝试着做,但目前只通过 whereHas 实现 B.product_name like 这部分的条件,当两个表都存在条件的时候,实在是做不出来。
望 Laravel 前辈们指点一下,谢谢!
PS. 补充:
目前是针对列表页做筛选检索,存在 paginate 的需求。
解决方法:
class Order extends Model
{
public function scopeProducts($query)
{
return $query->join('order_product', function($join) {
$join->on('order.order_id', '=', 'order_product.fk_order_id');
});
}
}Order::products()->where(....);
以上就是关于Laravel多个条件的关联查询问题 的内容,更多相关内容请关注PHP中文网(www.php.cn)!
相关文章:










