Question about the association query of multiple Laravel conditions?

Source: Internet
Author: User
Related queries for multiple Laravel conditions About the Association query of multiple Laravel conditions:

TableorderOrder table:

  • idAuto-increment ID

  • order_idOrder No.

  • paid_datePayment time

Tableorder_productOrder product table:

  • idAuto-increment ID

  • fk_order_idOrder Number, foreign key

  • product_nameName

  • product_numberNo.

  • quantityQuantity

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.