Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel模型关系中使用“where not”?_Php_Sql_Laravel_Model_Laravel 5 - Fatal编程技术网

Php 如何在Laravel模型关系中使用“where not”?

Php 如何在Laravel模型关系中使用“where not”?,php,sql,laravel,model,laravel-5,Php,Sql,Laravel,Model,Laravel 5,我的用户表类似于: === users === id: int (PK) name: string is_coach: bool ... === coach_requests === id: int (PK) student_id: int(FK => users.id) coach_id: int(FK => users.id) ... 和coach请求表,类似于: === users === id: int (PK) name: string is_coach: bool .

我的用户表类似于:

=== users ===
id: int (PK)
name: string
is_coach: bool
...
=== coach_requests ===
id: int (PK)
student_id: int(FK => users.id)
coach_id: int(FK => users.id)
...
和coach请求表,类似于:

=== users ===
id: int (PK)
name: string
is_coach: bool
...
=== coach_requests ===
id: int (PK)
student_id: int(FK => users.id)
coach_id: int(FK => users.id)
...
我也有相应的Laravel模型,即User和CoachRequest

在用户模型中,我希望创建一个方法,以便给定一个指定的用户,使用is_coach=true返回所有用户,除了:

他/她自己和 已作为coach_requests表中的coach与该人员匹配的用户。

例如考虑以下样本数据:

使用者

教练请求

现在,如果我是以下用户:

id 1即用户A,返回用户id:2、3、5和6 id 2,返回用户id:5,6 id 3,返回用户id:4,5 id 4,返回用户id:2,3 id 5,返回用户id:2、3、4 id 6,返回用户id:2、3、4 我如何使用Laravel做到这一点

到目前为止,我只知道:

public function scopeOfFreeCoaches($query) {
    return $query->where([
        'is_coach' => true,
    ]);
}
不多


非常感谢

这是我头上的一个原始查询:

SELECT u.id FROM users AS u
WHERE u.is_coach = true
AND u.id <> ’$userId’
AND u.id NOT IN(
    SELECT student_id FROM coach_requests
    WHERE coach_id = ’$userId’
)

快速完成且未经测试,因此您可能需要对其进行一些更改。

这是我脑海中的一个原始查询:

SELECT u.id FROM users AS u
WHERE u.is_coach = true
AND u.id <> ’$userId’
AND u.id NOT IN(
    SELECT student_id FROM coach_requests
    WHERE coach_id = ’$userId’
)

快速完成且未经测试,因此您可能需要对其进行一点更改。

多亏了@purpleinja raw query,我终于找到了使用Laravel执行此操作的方法:

public function getPotentialCoaches()
{
    return
        User::where('is_coach', true)
        ->where('id', '<>', $this->id)
        ->whereNotIn('id', function ($query) {
            $query->select('coach_id')
                ->from('coach_requests')
                ->where('student_id', $this->id);
        })
        ->get();
}

多亏了@purpleininja raw query,我终于找到了使用Laravel实现这一点的方法:

public function getPotentialCoaches()
{
    return
        User::where('is_coach', true)
        ->where('id', '<>', $this->id)
        ->whereNotIn('id', function ($query) {
            $query->select('coach_id')
                ->from('coach_requests')
                ->where('student_id', $this->id);
        })
        ->get();
}

在这种情况下,原始查询可能是最简单的方式,因为它是一种自定义的方式:我甚至不确定它的原始查询是什么。在这种情况下,原始查询可能是最简单的方式,因为它是一种自定义的方式:我甚至不确定它的原始查询是什么