i'm trying to save a new tvshow and it's episodes into the database with the following code but in the database show_id column is 0
//
$show = New Show();
$show->fill( $showInfo );
$show->save();
foreach( $episodes as $episodeInfo ) {
//
$showEpisode = New ShowEpisode();
$showEpisode->fill($episodeInfo);
//
$show->episodes()->save( $showEpisode );
}
i have also tried associating the show using the following code
//
$showEpisode = New ShowEpisode();
$showEpisode->fill($episode);
$showEpisode->show()->associate($show);
$showEpisode->save();
it seems that you cannot use a newly created model for association, but when using the below code to load the show model, the association works and in the database show_id is the correct value
$show = Show::find($showId);
is this how it is supposed to be? and should i be reloading the show from the database after inserting it ?
Question: how to associate a newly created show object with an episode
Found out the problem, the show object has to use increment id's
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community