0
votes

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?

1
Please share table structure. - Manisha
Hi @Ionut, welcome to SO. Please have a look on how to ask a good question and add missing information to your question. Currently there is a bit of information missing on what you already tried. Please be so kind and provide all the information you can give so others are able to help. - fragmentedreality

1 Answers

1
votes

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;