Search code examples
phplaraveleloquententrust

Eloquent IF clause - always returns true


I am using Entrust's default table structure:

permissions table:

+--------------+------------------+------+-----+---------+----------------+
| Field        | Type             | Null | Key | Default | Extra          |
+--------------+------------------+------+-----+---------+----------------+
| id           | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name         | varchar(255)     | NO   | UNI | NULL    |                |
| display_name | varchar(255)     | YES  |     | NULL    |                |
| description  | varchar(255)     | YES  |     | NULL    |                |
| created_at   | timestamp        | NO   |     | NULL    |                |
| updated_at   | timestamp        | NO   |     | NULL    |                |
+--------------+------------------+------+-----+---------+----------------+

permission_role table:

+---------------+------------------+------+-----+---------+-------+
| Field         | Type             | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+-------+
| permission_id | int(10) unsigned | NO   | PRI | NULL    |       |
| role_id       | int(10) unsigned | NO   | PRI | NULL    |       |
+---------------+------------------+------+-----+---------+-------+

roles table:

+--------------+------------------+------+-----+---------+----------------+
| Field        | Type             | Null | Key | Default | Extra          |
+--------------+------------------+------+-----+---------+----------------+
| id           | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name         | varchar(255)     | NO   | UNI | NULL    |                |
| display_name | varchar(255)     | YES  |     | NULL    |                |
| description  | varchar(255)     | YES  |     | NULL    |                |
| created_at   | timestamp        | NO   |     | NULL    |                |
| updated_at   | timestamp        | NO   |     | NULL    |                |
+--------------+------------------+------+-----+---------+----------------+

Now, given a role_id I'd like to get select the following from this database:

  • permissions.id
  • permissions.display_name
  • whether the permission_role table contains an entry with the permission_id and the given role_id

The last one turned out to be a bit tricky in Eloquent. This SQL query accomplishes exactly what I need (ID is obviously replaced by a valid role ID):

SELECT p.id, p.display_name, IF(pr.role_id = ID, 1, 0) AS has_role
FROM permissions p
LEFT OUTER JOIN permission_role pr ON p.id = pr.permission_id;

Example output:

+----+--------------+----------+
| id | display_name | has_role |
+----+--------------+----------+
|  1 | Edit users   |        1 |
|  2 | View users   |        0 |
|  3 | Delete users |        0 |
+----+--------------+----------+

Can anyone help me out here, on how to do this using Eloquent?

I've tried this, but it always returns 1 (true) in the third column, unlike the SQL query (as seen above).

$result = DB::table('permissions')
        ->leftJoin('permission_role', 'permission_role.permission_id', '=', 'permission_role.role_id')
        ->select(DB::raw('permissions.id, permissions.display_name, IF(permission_role.role_id = ID, 1, 0) AS has_role'))
        ->get();

Ideally, I'd like to do this without using DB::raw, although it is completely fine if that is what it takes.

Thanks in advance for any help!


Solution

  • Structurally, the Query Builder query you've shown looks fine.

    What does not look fine is the left join. Shouldn't this:

    ->leftJoin('permission_role', 'permission_role.permission_id', '=', 'permission_role.role_id')
    

    be this:

    ->leftJoin('permission_role', 'permission_role.permission_id', '=', 'permissions.id')
    

    ?