0

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.

Ruwan Liyanage
  • 333
  • 1
  • 13
  • `$car->wheelid_1 = $wheel1->id`, same with `wheel2`. – Tim Lewis Oct 21 '22 at 13:58
  • @Tim Lewis I need to reference the Wheel1 to the Car before saving any of them to the database. Both tables are using auto-id's. $wheel1->id is not available before saving the Wheel1. Please correct me if I got it wrong. – Ruwan Liyanage Oct 21 '22 at 14:02
  • So wait, does your `Wheel` model have a `car_id`, and your `Car` model have a `wheel1_id` and `wheel2_id`? ... Why? I think it would just make sense for that the connection to exist in 1 spot, like each Wheel has a `car_id`, and a Car has multiple wheels. I think you need to take a look at your database structure and refactor it a bit. This code won't work if you're expecting one to exist before the other, but the existence of both is dependent on the others existing... a "chicken before egg" scenario. – Tim Lewis Oct 21 '22 at 14:09
  • @Tim Lewis Wheel model only has the auto-id field, as the primary key. The car model has wheel1_id and wheel2_id. wheel1_id and wheel2_id are referenced by foreign keys to the Wheel table. I'm looking for something like the sample in my edited question. thanks. – Ruwan Liyanage Oct 21 '22 at 14:19
  • 1
    Then once again, the current code you have, plus `$car->wheel1_id = $wheel1->id` and `$car->wheel2_id = $wheel2->id` is what you need. You relate the models via IDs, and once saved, you can call `$car->wheel1` and `$car->wheel2`, which will return the `Wheel` models based on their IDs. Please read the documentation on Laravel's Models and Relationships; this is all detailed there. https://laravel.com/docs/9.x/eloquent and https://laravel.com/docs/9.x/eloquent-relationships. I would really consider refactoring your code though; `wheel1_id` and `wheel2_id` isn't a great approach. – Tim Lewis Oct 21 '22 at 14:23

1 Answers1

2

When you create a new model of item, in this case wheel. It gives you an id.

$wheel1=new Wheel();
$wheel1->save();
$wheel2=new Wheel();
$wheel2->save();

So you can use the model like this and retrieve the id

$wheel1->id;
$wheel2->id;
$car= new Car();
$car->name='Mazda';

if new car has property wheelid_1 & wheelid_2 you can save the ids of this wheel like so

$car->wheelid_1 = $wheel1->id;
$car->wheelid_2 = $wheel2->id;

$car->save();

A few bewares is that if you create a wheel model like that, make sure that in your migration the columns are set to nullable for no errors.

mexslacker
  • 23
  • 3
  • Also eloquent relationships must be defined in the model like so [link](https://laravel.com/docs/9.x/eloquent-relationships#one-to-one). In order to access the model after creating the relationship is like this `$wheel1Id = $car->wheel1->id;` – mexslacker Oct 21 '22 at 14:05