mercredi 24 février 2016

Laravel 5 relation between 3 tables

I have these 3 tables: users, recipesand votes

user votes on a recipe

a recipe has many votes

a user has many votes

a user can only vote once on a recipe

In my view i run into a problem, when I try to access the vote rating of a user on the recipe like:($recipe->user->vote->rating), it returns the first vote rating of that user (might be the vote of that user on another recipe)

my relations are: User.php:

public function vote()
{
    return $this->hasOne('App\Vote');
}

Recipe.php:

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

Vote.php:

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

public function recipe()
{
    return $this->belongsTo('App\Recipe','recipe_id');
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire