Model relation with multiple keys

Published 6 months ago by merisor

Hey guys,

This is probably trivial but i couldn't find an answer.

Let's say I have three Models

Order (order_id) OrderItems (order_id, item_id) OrderPromotion(order_id,item_id, other_fields)

Is there any way to make a relation from OrderItems to OrderPromotion

to be equivalent of select * from OrderPromotion where order_id = 11232 and item_id = 23432

thanks

Devmaurice

Add an order_item_id on the OrderPromotion table .

Dry7
Dry7
6 months ago (152,030 XP)

@merisor yes, https://laravel.com/docs/5.5/eloquent-relationships#many-to-many like this

class Order extends Model
{
    function promotions() {
        return $this->belongsToMany('App\OrderItems', 'OrderPromotion', 'order_id', 'item_id')->withPivot('other_fields');
    }
}

Please sign in or create an account to participate in this conversation.