omrakhur omrakhur - 2 months ago 10
MySQL Question

Laravel 5 - how to perform left join on same table

In my application, there's two tables at the moment:

users
and
employees
. The latter is simply a profile table for the boilerplate
users
table that ships with Laravel.

I have two foreign keys inside of
employees
, one is
user_id
that points to
id
on
users
table. So basically, the profile data of the registered user. The second foreign key in
employees
is
manager_id
. That is the id of the person who is the manager of an employee. Some users are managers and have a blank
manager_id
field.

I want to retrieve the row from my
Employee
model where the
user_id
matches the
manager_id
and then send them all to my view, where they will see the
first_name
and
last_name
of the manager. At the moment I see the manager ID instead of their name.

Answer

Sample LeftJoin =>

DB::connection('testing')->table('table1 as t1') ->select('t1.column, t2.column') ->leftJoin('table2 as t2','t2.client_id','=','t1.id') ->get();

Comments