Support the ongoing development of Laravel.io →
Database Eloquent

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

Last updated 3 years ago.
0
Solution

Found out the problem, the show object has to use increment id's

0

Sign in to participate in this thread!

Eventy

Your banner here too?

viiter viiter Joined 4 Nov 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.

© 2025 Laravel.io - All rights reserved.