Related queries for multiple Laravel conditions
About the Association query of multiple Laravel conditions:
Tableorder
Order table:
id
Auto-increment ID
order_id
Order No.
paid_date
Payment time
Tableorder_product
Order product table:
Table relationship:
order - 1:n - order_product
Requirements:
Use Laravel Eloquent ORM to implement the following native 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%'
I have read this manual several times and tried to do it. but currently, only the conditions for B. product_name like are implemented through whereHas. when both tables have conditions, it is impossible to do it.
Hope Laravel's predecessors can give me some advice. thank you!
PS. Supplement:
Currently, the list page is filtered and retrieved, and paginate is required.
Solution:
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(....);
The above content is about the association query of multiple Laravel conditions. For more information, see PHP Chinese website (www.php1.cn )!
Related articles:
Laravel associated query only obtains part of the data of the Management Object
Laravel Association query
Laravel