Php 访问范围内的透视

Php 访问范围内的透视,php,laravel,eloquent,Php,Laravel,Eloquent,我在这个问题上花了将近一个小时。我有一个用户模型和一个事件模型,与许多关系(表示邀请系统)链接。我为pivot类制作了一个模型: namespace App; use Illuminate\Database\Eloquent\Relations\Pivot; class Invitation extends Pivot { const PENDING = 0; const ACCEPTED = 1; const UNCERTAIN = 2; const REF

我在这个问题上花了将近一个小时。我有一个
用户
模型和一个
事件
模型,与许多关系(表示邀请系统)链接。我为pivot类制作了一个模型:

namespace App;

use Illuminate\Database\Eloquent\Relations\Pivot;

class Invitation extends Pivot
{
    const PENDING = 0;
    const ACCEPTED = 1;
    const UNCERTAIN = 2;
    const REFUSED = 3;
}
这是
用户
型号:

class User extends Authenticatable
{
    // Deleted useless things

    public function events()
    {
        return $this->belongsToMany('App\Event')
                    ->using('App\Invitation')
                    ->withPivot('status')
                    ->withTimestamps();
    }
}
class Event extends Model
{
    public function guests()
    {
        return $this->belongsToMany('App\User')
                    ->using('App\Invitation')
                    ->withPivot('status')
                    ->withTimestamps();
    }

    /**
     * Return a list of guests who accepted the invitation.
     */
    public function scopeAccepted($query)
    {
        return $query->wherePivot('status', Invitation::ACCEPTED);
    }
}
以及
事件
模型:

class User extends Authenticatable
{
    // Deleted useless things

    public function events()
    {
        return $this->belongsToMany('App\Event')
                    ->using('App\Invitation')
                    ->withPivot('status')
                    ->withTimestamps();
    }
}
class Event extends Model
{
    public function guests()
    {
        return $this->belongsToMany('App\User')
                    ->using('App\Invitation')
                    ->withPivot('status')
                    ->withTimestamps();
    }

    /**
     * Return a list of guests who accepted the invitation.
     */
    public function scopeAccepted($query)
    {
        return $query->wherePivot('status', Invitation::ACCEPTED);
    }
}
虽然这句话很管用:
$event=event::find(9)->guests()->wherePivot('status',Invitation::ACCEPTED)->get()

方法
scopeAccepted
给我这个错误:

使用
$event=event::find(9)->guests()->accepted()->get()调用未定义的方法illumed\Database\Query\Builder::accepted()

因为您必须在返回的
->guests()类型中定义
accepted
作用域,该类型的值为User:),所以我仍然收到一个错误:
SQLSTATE[42S22]:未找到列:“where子句”中的1054未知列“pivot”(SQL:选择'users`.''event\'user`.'event\'id`作为'pivot\'event\'id`、'event\'user`.'user\'id`作为'pivot\'user\'id`、'event\'user`.'created\'at`作为'pivot\'at`、'event\'user`.'updated\'u作为'pivot\'u\'u\'updated\'at`从'users'用户'内部加入'event\'u\'user\'user`.'id`.'event\'user\'user`.'user`.'id`.'event\'user\'user\'u\'u\'id`.'user`.'vent_id`=9和`pivot`=status)
如何在事件模型中创建一个只返回接受邀请的用户的函数?请尝试使用('App\invitation')->with pivot('status')来创建此函数->where('status','=',Invitation::ACCEPTED);->withTimestamps();}
并像这样调用它
$event=event::find(9)->acceptedGuests()->get();
!!太好了!我对它做了一些改进:
公共函数acceptedGuests(){return$this->guests()->('status','=',Invitation::ACCEPTED);}
乐意帮助:)因为您必须在返回的
->guests()类型中定义
ACCEPTED
作用域,该类型为用户:)我仍然收到一个错误:
SQLSTATE[42S22]:未找到列:“where子句”中的1054未知列“pivot”(SQL:选择'users`.''event\'user`.'event\'id`作为'pivot\'event\'id`、'event\'user`.'user\'id`作为'pivot\'user\'id`、'event\'user`.'created\'at`作为'pivot\'at`、'event\'user`.'updated\'u作为'pivot\'u\'u\'updated\'at`从'users'用户'内部加入'event\'u\'user\'user`.'id`.'event\'user\'user`.'user`.'id`.'event\'user\'user\'u\'u\'id`.'user`.'vent_id`=9和`pivot`=status)
如何在事件模型中创建一个只返回接受邀请的用户的函数?请尝试使用('App\invitation')->with pivot('status')来创建此函数->where('status','=',Invitation::ACCEPTED);->withTimestamps();}
并像这样调用它
$event=event::find(9)->acceptedGuests()->get();
!!太好了!我对它做了一些改进:
公共函数acceptedGuests(){return$this->guests()->('status','=',Invitation::ACCEPTED);}
乐意帮助:)