Support the ongoing development of Laravel.io →
Database Eloquent Architecture
Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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?

Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

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...

Last updated 1 year ago.
0

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
Last updated 1 year ago.
0

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!

Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

DomenicF domenicf Joined 2 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.