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 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?