时间:2021-07-01 10:21:17 帮助过:10人阅读
表 order 订单表:
id 自增ID
order_id 订单号
paid_date 支付时间
表 order_product 订单产品表:
id 自增ID
fk_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.gxlcms.com)!
相关文章:
Laravel 关联查询只获取管理对象的部分数据
laravel 关联查询问题
laravel 关联查询文章和文章作者