So I'm trying to make an app that organizes movies and tv. Right now I'm focusing on Movies, but keep in mind the goal is to have a design that can be used for both.
The app should be able to do things like "get the people who acted in this movie", or "get all people who are directors". But people can be actors, directors, writers, producers, and any combination thereof on any number of different movies (and later, tv episodes). So far I've only gotten this functionality by having two models Person and Movie, and then in the database I have tables called actors, directors, writers. Each of these tables has only the fields person_id and movie_id, so they look like pivot tables but aren't actually used as such (red flag #1). Instead, the concept of "what this person did on this movie" is abstracted away into Controllers that do subqueries on these pivot-but-not-really tables. So my routes file looks like this:
Route::get('/movies', 'MovieController@index');
Route::get('/movies/{id}/actors', 'MovieActorsController@index');
Route::get('/movies/{id}/directors', 'MovieDirectorsController@index');
Route::get('/people', 'PersonController@index');
Route::get('/actors', 'ActorController@index');
Route::get('/directors', 'DirectorController@index');
And the controllers look like this:
class MovieActorsController extends Controller
{
// Get people who acted in the specified movie
public function index($id)
{
return Person::whereIn('id', function($sub) use($id) {
$sub->select('person_id')->from('actors')->where('movie_id', $id);
})->get();
}
}
Obviously I really want these relationships to actually exist in the database, and in my app as Eloquent models. Maybe we have a Credit model, and each Person and Movie model can have many Credits. But each Credit can be one of any different kinds like ActingCredit, DirectingCredit, etc. I've read the Eloquent docs and a lot of blog posts/forum comments, and I keep feeling like I'm getting closer but not quite there. I think the answer lies in custom Pivot table Models and/or polymorphism but I'm hitting a mental roadblock as to how to actually implement them.
Any help -- even just terms and phrases to google -- would be greatly appreciated. Thanks!
Edit: If it's not clear what problem I am trying to solve, please see my later reply here https://laracasts.com/discuss/channels/eloquent/how-can-i-define-these-kinds-of-relationships-in-eloquent?page=1#reply=541470