If I have a two models, with a hasMany relationship, so my Car
hasmany CarOwners
, I would like to use to use the firstOrCreate
method to create a new car
.
If I'm creating a new car
then I need to create the first CarOwner
too.
Without leveraging any laravel functionality, I would
car
Isn't there a way to do this in one go? I can do the queries to find the CarOwners of the chosen car, but I can't see how to save across multiple tables.
Refer to laravel manual .if you already created a Model for them it would be like this
car->carOwners()->save($owners)
link https://laravel.com/docs/5.1/eloquent-relationships#inserting-related-models
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With