Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fabiomingorance's avatar

Inserting data - hasOne relationship

Hello, I am developing this structure of relationship for registering suppliers in my app:

Entity hasOne > Company hasOne > Supplier

What would be the best practice on inserting data with this structure since a supplier is only a supplier if these 3 tables match. I am trying different approaches but all of them are flooding my controller with dozens of lines of code and it doesn't seem right.

0 likes
2 replies
fabiomingorance's avatar

Yes, the data comes from a form, the problem is that the first entry needs to be in table Entity, then Company and then Supplier, like this:

$entity = new Entity(); $entity->save();

$company = new Company(); $company-entity_id = $entity->id; $company->save();

$supplier = new Supplier(); $supplier->entity_id = $entity->id; $supplier->legal_name = Request::input('legal_name'); $supplier->email = Request::input('email'); $supplier->phone = Request::input('phone'); etc... $supplier->save();

Is there an alternative way to achieve this in Laravel?

Please or to participate in this conversation.