简体   繁体   English

使用where子句从Laravel中的联接查询获取数据

[英]use where clauses to get data from join query in Laravel

I am creating a system using Laravel and AngularJS where I assign tasks to users. 我正在使用Laravel和AngularJS创建一个系统,在其中向用户分配任务。 Multiple tasks has multiple users and vice versa. 多个任务有多个用户,反之亦然。 In the Database, I have this tables: 在数据库中,有以下表格:

task: id | 任务:id | name 名称

task_users: id | task_users:id | task_id | task_id | user_id 用户身份

users: id | 用户:id | name 名称

In my view, I display a particular task, using id of task table. 在我看来,我使用任务表的ID显示特定任务。 I display a list of users (called unassigned users) who are not assigned to that particular task. 我显示未分配给特定任务的用户(称为未分配用户)列表。 When that user is assigned, it's name gets removed from the list. 分配该用户后,其名称将从列表中删除。

To achieve this, I used this query: 为此,我使用了以下查询:

    public static function remainingUser($task_id)
    {
    return \DB::table('users')
        ->leftjoin('task_users', 'task_users.user_id', '=', 'users.id')
        ->select('users.id',
            'users.name as name'
        )
        ->where('task_id', '!=', $task_id)
        ->orWhere('task_id', null)
        ->get();
    }

Suppose I have this data 假设我有这个数据

task: 任务:

id | id | name 名称

1 | 1 | Task1 任务1

2 | 2 | Task2 TASK2

3 | 3 | Task3 TASK3

users: 用户:

id | id | name 名称

1 | 1 | User1 用户1

2 | 2 | User2 用户2

3 | 3 | User3 用户3

4 | 4 | User4 用户4

5 | 5 | User5 用户5

6 | 6 | User6 User6

7 | 7 | User7 User7

8 | 8 | User8 User8

9 | 9 | User9 USER9

10 | 10 | User10 User10

task_users: task_users:

id | id | task_id | task_id | user_id 用户身份

1 | 1 | 1 | 1 | 1 1

1 | 1 | 1 | 1 | 2 2

1 | 1 | 1 | 1 | 3 3

1 | 1 | 1 | 1 | 5

1 | 1 | 1 | 1 | 6 6

1 | 1 | 1 | 1 | 7 7

1 | 1 | 2 | 2 | 2 2

1 | 1 | 2 | 2 | 4 4

Now suppose I am displaying task details of task_id = 1 and I want to assign user 4 to this task. 现在,假设我正在显示task_id = 1的任务详细信息,并且我想将用户4分配给该任务。 So my list of unassigned users should contain all the users who are not assigned this task. 因此,我的未分配用户列表应包含未分配此任务的所有用户。 My query does not return the required data. 我的查询未返回所需数据。 I have also tried different conditions in where clause, but I do not get the correct required data. 我还在where子句中尝试了不同的条件,但是没有获得正确的必需数据。 What am I doing wrong? 我究竟做错了什么?

The issue exists because when you select from Users , you get all users left Joined with a single instance of tasks. 之所以存在此问题,是因为当您从Users选择时,您使所有用户left Joined了单个任务实例。 So if User1 has Task1 and Task2 , only Task1 will be matched here, because it will match User1 to the 1st row found for him within task_users . 因此,如果User1具有Task1Task2 ,则此处仅Task1将被匹配,因为它将User1匹配到task_users为他找到的第一行。 In order to list the unasigned users, your query would look similar to this: 为了列出未分配的用户,您的查询将类似于以下内容:

public static function remainingUser($task_id)
{
return \DB::table('task_users')
    ->rightJoin('users', 'task_users.user_id', '=', 'users.id')
    ->select(
      \DB::raw("DISTINCT(`users`.`id`)"),
      'users.name as name'
    )
    ->where('task_id', '!=', $task_id)
    ->orWhere('task_id', null)
    ->get();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM