Laravel 4: Adding where clause to a join condition
if you want add more condition on a join
add more $join->on
or $join->orOn
.
if you want to add a condition to your first select, add it outside join function.
DB::table('users')->join('contacts', function($join){ $date = date('Y-m-d'); $join->on('users.id', '=', 'contacts.user_id');})->where('contacts.effective_date', '>=', $date);->get();
Updated
In Laravel 4.0 which I think you use, you can't use where
inside your join closure, but since Laravel 4.1 and above you can have where
conditions after your join condition. I couldn't find documentation for Laravel 4.1 but this is the #join documentation for L4.2 and above
Try This solution
DB::table('users') ->join('contacts', function($join) { $current_date = date('Y-m-d'); $join->on('users.id', '=', 'contacts.user_id') ->where('contacts.effective_date', '>', $current_date) ->where('contacts.effective_date', '=', $current_date); }) ->get();