Rename Pivot Table

If you want to rename "pivot" word and call your relationship something else, you just use ->as('name') in your relationship.

Model:

1public function podcasts() {
2 return $this->belongsToMany('App\Podcast')
3 ->as('subscription')
4 ->withTimestamps();
5}

Controller:

1$podcasts = $user->podcasts();
2foreach ($podcasts as $podcast) {
3 // instead of $podcast->pivot->created_at ...
4 echo $podcast->subscription->created_at;
5}

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord