Laravel 5.6 Eloquent关联创建失败(类型错误)求助
解决Laravel多对多关联中的类型错误
先看看你遇到的错误:
Type error: Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::save() must be an instance of Illuminate\Database\Eloquent\Model, integer given, called in /home/sasha/Documents/OffProjects/vetnearme/vetnearme/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php on line 814
这个错误的根源在你的HasPermissionsTrait里的giveRole()方法,我来帮你拆解问题:
问题所在
你用\DB::table('roles')->where('name', '=', $role)->first()查询角色时,得到的是一个普通的PHP对象(StdClass),而不是Laravel的Eloquent模型实例。更关键的是,你把$role->id(一个整数)传给了saveMany()方法——但BelongsToMany关联的save()/saveMany()方法要求必须传入Eloquent模型实例,不能直接传ID,这就触发了类型错误。
修复方案
我们可以用两种方式修复这个方法,推荐第二种,更贴合多对多关联的使用场景:
方式一:用Eloquent模型查询并传递实例
修改giveRole()方法,用Role模型来查询角色,然后直接把模型实例传给saveMany():
public function giveRole($role = 'user') { // 用Role模型查询,得到Eloquent实例 $role = Role::where('name', '=', $role)->first(); // 传递模型实例数组给saveMany $this->roles()->saveMany([$role]); return $this; }
方式二:用attach()方法(更推荐)
对于多对多关联,Laravel提供了attach()方法,专门用来附加已存在的关联模型,它支持直接传模型ID或者模型实例,用法更简洁:
public function giveRole($role = 'user') { // 可以直接查ID,或者查模型实例 $roleId = Role::where('name', '=', $role)->value('id'); // 用attach附加关联 $this->roles()->attach($roleId); // 或者直接传模型实例也可以 // $role = Role::where('name', '=', $role)->first(); // $this->roles()->attach($role); return $this; }
额外的小问题提醒
我还注意到你在hasPermissionThroughRole()方法里有个笔误:
// 错误写法:$this->role->contains($role) if($this->role->contains($role)) return true;
应该改成复数形式$this->roles,因为你的关联方法是roles()(多对多):
if($this->roles->contains($role)) return true;
这样调整后,你的用户注册时给角色的逻辑就能正常运行了~
内容的提问来源于stack exchange,提问作者Sasha
相关产品推荐
相关产品推荐

