mardi 10 juillet 2018

How to get all books from another table when using many to many relationship Laravel / Eloquent / Query builder

Let me explain the scenario.

i have tables:

competitions

id title body

then

books

id name user_id

then i have a pivot table to store participants

so

participants

id competition_id user_id

Now i have set some relationships for these models.

Competition model

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

User model

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

Now i am fetching one single competition and in that same query i need to fetch a list of books of the participants of that competition. So how can i achieve this. Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire