Laravel, Eloquent: SQL query with left join

If the query needs parentheses/brackets for a where condition like below Normal SQL:

SELECT users.fname, planets.name 
FROM  users
LEFT JOIN  planets ON (users.planet_id = planets.id)
WHERE users.lname LIKE '%hawk%' 

ELOQUENT SQL:

$name = "hawk";

$userResults = DB::table('users')
       ->leftJoin('planets', function($join) {
         $join->on('users.planet_id', '=' , 'planets.id');
        })
       ->where('users.lname' , 'like', '%'.$name.'%')
       ->get(array('users.fname', 'planets.name'));

3 comments:

  1. Is this eloquent or query builder?
    Check https://laravel.com/docs/5.1/queries#joins

    ReplyDelete
  2. Clearly this is NOT Eloquent. Eloquent is Laravel's Model, ActiveRecord. If you start the code with DB, then it's just the Query Builder.

    ReplyDelete

Python contextlib for Timing Python code

If you've ever found yourself needing to measure the execution time of specific portions of your Python code, the `contextlib` module o...