A relation uses the parent id and in child a foreign id.
id would point to company_id // whatever the foreign key is
Any other fields would be there to use as needed. Have you practiced some of the examples from:
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Greetings!
I'm developing a Laravel application with just 02 models. One is called Company and another is called Customer. I have a "belongsTo"relation between Customer and Company for each User. For example:
Each company has a code that is a combination os 03 columns in database: cnpj_base, cnpj_ordem and cnpj_dv. In my Customer database my relation poinst to the combination of this 03 columns. For example:
I have the company Lacacasts, which has the following code: cnpj_base = 1234 cnpj_ordem = 0000 cnpj_dv = 01
In my Customer database I have the following scenario: user_id = 4 company_code = 1234000001
How do I make a belongsTo relation in Customer model to link my Customer to the Company, like that?
$customer->company; // returns the company
Please or to participate in this conversation.