我有两个使用多对多关系的模型。
class Tag extends Model
{
protected $table= 'tags';
protected $primaryKey = 'id';
protected $fillable = [
'name'
];
public function members()
{
return $this->belongsToMany('App'Data','data_tag','tag_id','data_id')
->withTimestamps();
}
}
以及数据模型。。
class Data extends Model
{
protected $table= 'dbaccess';
protected $primaryKey = 'id';
protected $fillable = [
'username','password','email','added_at','user_id'
];
/**
* @return 'Illuminate'Database'Eloquent'Relations'BelongsToMany
*/
public function tags()
{
return $this->belongsToMany('App'Tag','data_tag','data_id','tag_id')
->withTimestamps();
}
}
其中CCD_ 1正在链接表。
当我调用函数时
$mani = App'Data::find(2);
然后
$mani->tags()->attach(3);
我得到以下错误。
[Symfony'Component'Debug'Exception'FatalErrorException]
Call to undefined method Illuminate'Database'Eloquent'Collection::tags()
有人能帮我吗?
dd($mani)回复此
看起来你得到的是一个集合,而不是一个模型
尝试使用获取集合的第一个元素
$mani = App'Data::find(2)->first();