我尝试向belongsTo关系中的attach()方法添加一个属性,但它没有任何影响:
// User Model
class User extends Eloquent {
public function roles(){
return $this->belongsToMany(`App\Role`);
}
}
// Role Model
class Role extends Eloquent {
public function users(){
return $this->belongsToMany(`App\User`);
}
public function attach($id, array $attributes = [], $touch = true){
$attributes['foo'] = 'bar';
parent::attach($id, $attributes, $touch);
}
}当我执行$user->roles()->attach($id, ['key' => 'value']);时- ['key' => 'value']在那里,但它没有我在扩展连接方法中设置的['foo' => 'bar'] (我相信它没有被调用)。
发布于 2015-09-15 14:33:13
首先,如果您正在处理多对多关系,则必须使用attach method only。
更多信息请点击此处:http://laravel.com/docs/5.1/eloquent-relationships#inserting-many-to-many-relationships
因此,您有两种不同的选择:
https://stackoverflow.com/questions/32575691
复制相似问题