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

Install CMake 3.30.8 on Ubuntu 22.04

How to Install CMake 3.30.8 on Ubuntu 22.04 CMake is a powerful open-source tool for managing the build process of software proje...