不能在laravel中使用attach()方法

时间:2017-02-27 14:41:51

标签: php laravel-5

我设置了以下关系:

public function notes()
{
    return $this->belongsToMany(Note::class);
}

public function tags()
{
    return $this->belongsToMany(Tag::class);
}

和像这样的数据透视表:

Schema::create('note_tag', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->integer('note_id')->unsigned()->index();
        $table->foreign('note_id')->references('id')->on('tags')->onDelete('cascade')->onUpdate('cascade');
        $table->integer('tag_id')->unsigned()->index();
        $table->foreign('tag_id')->references('id')->on('notes')->onDelete('cascade')->onUpdate('cascade');
    });

现在,我使用了Attach()方法:

$note->tags()->attach($tagsIds);

但是这不起作用,我收到了这个错误:

  

[Symfony的\元器件\调试\异常\ FatalErrorException]    无法实例化接口phpDocumentor \ Reflection \ DocBlock \ Tag

1 个答案:

答案 0 :(得分:1)

您似乎导入了与 Tag :: class 对应的错误类。

应该是这样的:

  

使用App \ Tag;

而不是:

  

使用phpDocumentor \ Reflection \ DocBlock \ Tag;