Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Providing more information could help in getting a reply, as there are a few ways to insert data in to a database using Laravel.

How are you inserting.... DB::TABLE.... Model::create ... $var = new Model()... etc.

0

I'm doing this:

    $show = new Show;
    $show_title = $show->ShowTitle ?: new ShowTitle;
    $show_title->data_entered = Input::get('data_entered');
    $show->save();
    $show->new_title()->save($show_title);

I need to get the ID for the record created in ShowTitle.

The reason being is that Show needs to be related to the new record in ShowTitle (id in Show needs to be the same as show_id in ShowTitle). But the ID for the new record in ShowTitle also needs to related to Show (id in ShowTitle needs to be the same as title_id in Show).

Last updated 8 years ago.
0

Sorry, yesterday ended up being a very busy.

If your relationships are setup in the models, you should be able to simply save the records and Laravel will handle the id's.

You might try a dd of the $show after the first save, to see if the id was was created in $show - it should have one.

$show->new_title()->save($show_title) , should save the $show_title model record and assign the id of the $show record to show_id in the $show_title model.

I'm assuming the new_title() is a Laravel relationship, hasMany, belongsTo, etc..

A dd of $show->toArray() after the new_title()->save might also show if the new_title() has any data in it.

Humm, since you make a change to the $show after the first save, you might do a $show->save() after the new_title()->save($show_title), I am drawing a blank on if does this automatically, but I couldn't hurt.

Not sure what else to offer currently, ... maybe turning on DB logging and seeing what queries are being run... or double checking the relationship...

Hope that helps.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

jerauf jerauf Joined 16 Feb 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.