Hey guys,
I'm trying to figure out if this is the best way to go about this. I have a tv_shows
table and a movies
table, along with a genres
table. I want to use the genres from the genres table in both the TV Shows/Movies models. Do I need to use many to many polymorphic relationships?
If I do, the documentation is a bit blurry (http://laravel.com/docs/eloquent#polymorphic-relations); there's a taggable_id
column within the taggables
table in this example, is this the ID pointing to either the post or the video? In my case, it would be the tv show or the movie?
Thanks a lot!
Can tv_shows
and movies
have multiple genres? if so, you'll need many to many for each, if they only have one genre you'll just need a one to many relationship between tv_shows
-> genres
and movies
-> genres
.
Yeah, tv_shows
and movies
can have multiple genres, or at least I want to set it up that way. What you're saying is I need two normal many to many relationships, with two pivot tables, e.g. movie_genres
and tv_genres
?
Think about just having one pivot table, how do you will manage the ids for tv and movies in a single row, ie:
tv_id | movie_id | genre_id
-------------------------------------
1 null 1
null 1 1
you will end up storing bytes that you don't need,
even more, if you need to add short
category later, you will need to modify pivot table's schema, not a very good approach
martin has the right answer
I'm a little confused now, are you suggesting that I should use a single pivot table or are you going against it? Is what I said in my previous post (2 tables) what you think is a good idea? I see a few different scenarios...
Like what you said, 1 table:
tv_id | movie_id | genre_id
-------------------------------------
1 null 1
null 1 1
The polymorphic many-to-many approach that Laravel shows:
genre_id | genre_relationship_id | genre_relationship_type
--------------------------------------------------------------------------------------
1 1 TV
1 1 Movie
Or multiple pivot tables:
genre_id | movie_id
--------------------------------------------------------------------------------------
1 1
2 3
genre_id | tv_show_id
--------------------------------------------------------------------------------------
1 1
5 1
I might be overthinking this, but I just want to understand this better. Why is one way a better approach than another, etc. Multiple tables seems silly, if I need to make a change on a pivot table I would need to make it in multiple places. Polymorphic many-to-many seems kind of advanced, but also seems like what I'm trying to do. One table seems a bit strange because the composite keys would go across three rows and I couldn't null one unless I just programatically did everything, forgetting about indexes...
thinking more about your problem, try this
table categories
id name
1 'movie'
2 'tv'
table genres
id name
1 'sci-fi'
2 'horror'
table shows
id name category_id
1 'Prometheus' 1
2 'V Invasion' 2
table shows_and_genres
show_id genre_id
1 1
1 2
2 1
Yeah, but in this case, everything would be a "show". I was thinking about doing this actually, might be the best option... I just wish I could have a model for a TV show and a movie. I'll give it a shot. Thanks!
make an abstract class
called Show
abstract class Show extends Eloquent {
protected $table = 'shows';
// put here common methods for movies and tv
}
then for movies
class Movies extends Show {
// override get method, so always all you queries will contain this where clause
public function get() {
return parent::where( 'category_id', 'movie' )->get();
}
// add methods unique for movies
}
same similar goes for tv
arcollector, thank you so much. You've given me such a great direction to move forward with. Appreciate the time you've taken to explain everything to me. I'm going to try and set up a repository and do as you suggested, it seems like a foolproof method of going about things.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community