Reputation: 1748
I have the following table structure
turns
id
orders_payments
id
turn_id
order_id
orders
id
And I want to get all orders related to a turn
so
Class Turn{
public function orders(){
return ????
}
}
How can you achieve that?
I tried the hasmanythrough
but it only works when the relations are in cascade
Thanks!
Upvotes: 1
Views: 7956
Reputation: 1748
As @lowerends points out, this is a BelongsToMany relationship, since I was using the orderPayments table for more things I didn't notice it until he said, so finally the solution is the following
```
public function orders(){
return $this->belongsToMany('Order','orders_payments','turn_id','order_id');
}
```
Upvotes: 1
Reputation: 81167
I wonder why you make your life hard and call your model Turn
and the pivot table order_payment
?
Anyway, you want this:
// Turn model
public function orders()
{
return belongsToMany('Order', 'order_payment');
// in case you would like to call your fkeys differently, use this:
// return belongsToMany('Order', 'order_payment', 'payment_id', 'order_whatever_id');
}
// Order model
public function turns() // or payments() ?
{
return belongsToMany('Turn', 'order_payment');
}
Upvotes: 7
Reputation: 5267
You can use
public function orders()
{
return $this->belongsToMany('Order');
}
Then you can do
$orders = Turn::find(1)->orders;
Upvotes: 1