Reputation: 37
I've a problem in laravel eloquent.
I have 3 tables: members
, subscriptions
and a pivot table for a many-to-many relationship between members and subscriptions: member_subscription
.
I have another table that is tied on member_subscription
, with check-ins for member subscription.
What I need is to define the relationship between member_subscription
and check-ins
. I read something about a custom model for the pivot table, how can I achieve this?
Upvotes: 0
Views: 405
Reputation: 10186
You can create a model for the member_subscription
. Then, when you query the relationship between members
and subscriptions
, you can use the using
function.
In the Member model:
public function subscriptions()
{
return $this->belongsToMany('App\Subscription')->using('App\MemberSubscription');
}
In the Subscription model:
public function members()
{
return $this->belongsToMany('App\Member')->using('App\MemberSubscription');
}
Now, in your MemberSubscription
model, define the relationship to checkins:
public function checkins()
{
return $this->hasMany('App\Checkins');
}
Now you can do stuff like:
$member->subscriptions()->first()->pivot; // Returns a MemberSubscription model
$member->subscriptions()->first()->pivot->checkins;
Upvotes: 1