lundi 13 novembre 2017

Laravel 5.2 Many to Many query relationship with where clause

I have a little problem, I have a table with Movies and another with Genres ex: Action, Adventure etc...

Table names:

  • movies
  • movie_genre
  • genres

I want to fetch all the Movies with the selected Genre ex: Action

$movies = Movie::with('genres')
            ->when($genre, function ($query) use ($genre) {
                        return $query->where('genres.id', $genre->id);
                    })
            ->paginate(20);

This php code doesn't work, how can I make it work?

PS: The genre is transfered from the view by $_GET['genre'] and stocked in the variable $genre.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2iaZf7m
via IFTTT

Aucun commentaire:

Enregistrer un commentaire