I'm new to Laravel, and trying to save the Car with a specific Wheels as follows;
$wheel1=new Wheel();
$wheel1->save();
$wheel2=new Wheel();
$wheel2->save();
$car= new Car();
$car->name='Mazda';
$car->wheelid_1='1';
$car->wheelid_2='2';
$car->save();
The problem I'm having is, I need to save the car, wheel1, and wheel2 objects at the same time without referring to or knowing their id's. But I have no wheelid_1 and wheelid_2 until I save the wheel1 and wheel2 first.
I referred this, this and other similar questions but was unable to figure out how to assign wheel1 and wheel2 as new related objects to the car model.
I have done similar tasks using Entity Framework, by just assigning child objects to relevant properties of the parent object with C#. Can I use a similar method in Eloquent?
I have added foreign keys for Wheels and Car on the table creation.
How can I make the reference between all these 03 objects without using their ids for saving?
What I can imagine is something like the below;
$wheel1=new Wheel();
$wheel2=new Wheel();
$car= new Car();
$car->name='Mazda';
$car->wheel1=$wheel1;
$car->wheel2=$wheel2;
$car->save();
Any help would be highly appreciated.