Reputation: 11691
I have this MYSQL query and require to formulate this using Eloquent in Laravel4.
SELECT DISTINCT Employee.Name, Employee.Id FROM Employee
LEFT JOIN Team ON Employee.Id = Team.EmployeeId AND Team.TeamId = 1;
AND
clause on the JOIN
in Laravel. Upvotes: 2
Views: 2151
Reputation: 12503
Create your Employee model with convention or proper $table
connection and then:
Employee::select(array(DB::raw('DISTINCT `Employee`.`Name`'), 'Employee.Id'))
->leftJoin('Team', function($join)
{
$join->on('Employee.id', '=', 'Team.EmployeeId')
->on('Team.id', '=', DB::raw(1));
})
->get();
If you want to use just Query Builder
(Fluent) then replace Employee::
with DB::table('Employee')->
.
Notes:
DB::raw()
instructs Laravel not to put
backtics there.join
methods you can add more join conditions to it, on()
will add AND
condition and orOn()
will add OR
condition.Upvotes: 8