首页 > 解决方案 > Laravel - 附加方法抛出异常

问题描述

我得到了以下表格

我想,给定一个演员,在 actor_stat 中插入一个元素。

控制器中的代码

$actor = Actor::where('name', $actor->name)->first();
$actor->stats()->attach(10); //10 is the id of an existing stat
$actor->save();

演员模型

class Actor extends Model
{
    public function stats()
    {
        return $this->belongsToMany('App\Stat', 'actor_stat')->withPivot('quantity');
    }
}

统计模型

class Stat extends Model
{
    public function users()
    {
        return $this->belongsToMany('App\User', 'actor_stat')->withPivot('quantity');
    }
}

问题是它抛出异常,我无法读取日志,因为权限被拒绝。建议?

标签: phplaraveleloquent

解决方案


发现了问题,我在数据透视表中没有可以为空的属性,所以我必须给它们一个值。


推荐阅读