dongmi1872 2016-02-08 20:29
浏览 49
已采纳

Eloquent IF子句 - 始终返回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!

  • 写回答

1条回答 默认 最新

  • duangai9678 2016-02-08 21:59
    关注

    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')
    

    ?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?