返回在透视表中创建记录的实例



我有一个数据透视表来创建一个跟随系统。

当我创建一个新追随者的记录时,我需要得到该记录的实例。

模型用户

public function followers()
{
return $this->belongsToMany(User::class, 'followers', 'leader_id', 'follower_id');
}

我创建这样的记录:

$this->user->followers()->attach(auth()->user()->id);

但是,如果我尝试像这样返回实例,它什么也不返回。

$follow = $this->user->followers()->attach(auth()->user()->id);

我还能怎么做?

Attach函数不返回任何内容。如果你想得到你的支点试试:

$userFollowerPivot = $this
->user
->followers()
->where('followers.id', auth()->user()->id)
->first()
->pivot;

最新更新