Skip to content
Advertisement

Laravel 7 relationship query data not working

I would like to query companies name through employees. Why didnt get any result? It not give any error.

Companies model

JavaScript

Employees model

JavaScript

Employees controller

JavaScript

Where i like to query

JavaScript

This is the migrations for tables

JavaScript

Advertisement

Answer

please provide the foreign key to your relation …

JavaScript

this relation means that the employee has only one company (belongs To one company)

so you can’t iterate over it!

in this line:

JavaScript

employe->companies will return only one object .. if you want to show all companies as options, pass $allCompany like:

JavaScript

then you can iterate $compnanies in your view

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement