Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

testy's avatar

Laravel 5 problems with relations pivot

I 'm just not on the solution :(

I have 3 Tables

- users (id, username, ...)
- entrys (id, title, ..)
- favourites (id, user_id, entry_id)

I need that favourites table is a pivot table from users and entrys.

My Models

class User extends Model {

  public function entrys() {
     return $this->hasMany('App\Entrys');
   }

    public function favourites() {
      return $this->hasMany('App\Favourite');
    }
}

class Entrys extends Moldel {

  public function favourite() {
    return $this->hasMany('App\Favourite');
  }

  public function user()
  {
    return $this->belongsToMany('App\User');
  }
}

class Favourite extends Model {

   public function entrys() {
      return $this->belongsTo('App\Entry');
    }

    public function user() {
      return $this->belongsTo('App\User');
    }
}

0 likes
11 replies
bestmomo's avatar

I think you just need that :

class User extends Model {

  public function entrys() {
     return $this->belongsToMany('App\Entrys', 'favourites');
   }

}

class Entrys extends Moldel {

  public function users()
  {
    return $this->belongsToMany('App\User', 'favourites');
  }
}
JarekTkaczyk's avatar

@testy A few things:

  1. you need belongsToMany relation on both sides, no need for Favourite model at all (unless you need it, ie. favourites is not just pivot table)
  2. You need to specify pivot table name in the belongsToMany since it is not following the convention
  3. I suggest you rename the entrys table and definitely Entrys model, because now it's: incorrect & not meningful
  4. If you really need Favourite model, then it has belongsTo relations to both User and Entry, and there will be hasMany on the other sides
class User extends Model {

  public function entries() {
     // you need to specify the pivot table, since eloquent would search for entry_user as default
     return $this->belongsToMany('App\Entry', 'favourites'); 
   }

}

class Entry extends Model {

  // if you rename the table to entries, then it's not necessary. 
  // Otherwise you need to specify the table:
  protected $table = 'entrys';

  public function users()
  {
    return $this->belongsToMany('App\User', 'favourites');
  }
}

// now:
$entry = Entry::find($someId);
$entry->users; // collection of users
User::find($userId)->entries; // collection of related entries
2 likes
testy's avatar

Mhhh...i stuck :(

I need this:

$allEntry = Entry::all();

foreach($allEntry as $entry) {
  echo $entry->user->favourites;
}

So this should check if a user has favourites...but i habe big problems to get it work :( @JarekTkaczyk, i tried your solution, but this not work for my case :(

JarekTkaczyk's avatar

@testy You're not doing it right. A favourite in this schema means, that a User has related Entry (or entries for that matter).

So here's something like what you asked for:

$users = User::with('entries')->get();

foreach ($users as $user)
{
  if (count($user->entries))
  {
    // user has favourited at least one Entry, ->entries is full collection of all of them
  }
  else
  {
    // user have no favourite entries
  }
}
1 like
testy's avatar

Thanks for your big help, but...

i make this:

  $entrys = Entry::with('users')->get();

  @foreach($entrys as $entry)
     Entry title: {{ $entry->title }}
     User has this entry as favourite? {{ $entry->user->favourite === $entry->id ? 'yep' : 'no' }}
  @endforeach

But this not work

JarekTkaczyk's avatar

@testy I told you it was not right. So instead tell me what you want to achieve - without pasting any code. Just say. For example who is ->user ?

Now, this is incorrect because obviously $entry->user is not there, $entry->users would be ok, but it is a collection, so you can't call $entry->users->favourite...

1 like
testy's avatar

Ok, i will output a list of entrys. On the right is an icon that show if the current user has this entry as favourite. (In the favourites table). But I would also have access to all of a user's favourites ( in another view).

JarekTkaczyk's avatar
Level 53

@testy Yeah, that's what I meant. Here it goes then:

// Assuming $user is current user, eg. got from Auth::getUser()
$favourites = $user->entries()->select('entries.id')->lists('id');

// show all with additional info if favourited
@foreach ($entries as $entry)
  Title: {{ $entry->title }}
  User has this entry as favourite? {{ in_array($entry->id, $favourites) ? 'yep' : 'no' }}
@endforeach

// another view - show only favourited
@foreach ($user->entries as $entry)
  Title: {{ $entry->title }}
@endforeach
2 likes
RailsRunner's avatar

There is a typo "class Entrys extends moldel" needs to be "class Entrys extends model"

Please or to participate in this conversation.