I have the following tables:
- Users;
- Companies;
- Employees.
I'm trying to bring all the employees of a company. In SQL it's pretty easy:
SELECT c.name, eu.name FROM users u
JOIN companies c ON c.user_id = u.id
JOIN employees e ON e.company_id = c.id
JOIN users eu ON eu.id = e.user_id
WHERE u.id = 2
But in Laravel I have tried everything and can not get this listing (company name + employee name) correctly.
Here are the frustrated attempts:
-
In this I tried to get all the companies the user logged in to then list all the employees.
$companies = Auth::user()->with('companies')->with('employee')->where('id', '=', Auth::user()->id)->get();
-
Here I started with the Employee model.
$employees = Employee::with('company')->with('user')->where('user_id', Auth::user()->id)->paginate(30);
As the platform is multi user, it is interesting to remember that:
- A User has N Companies;
- A Company has N Employees;
- And an Employee belongs to ONE User.
Hence comes the circular structure of the tables.
Thank you.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire