narges narges -4 years ago 122
PHP Question

cannot using attach() method in laravel

I have the following relations set up:

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

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


and pivot table like this:

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');
});


now, i used Attach() method:

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


but that doesn't work and I get this error:


[Symfony\Component\Debug\Exception\FatalErrorException]
Cannot instantiate interface phpDocumentor\Reflection\DocBlock\Tag

Answer Source

Looks like you've imported the wrong class that corresponds to Tag::class.

It should probably be something like this:

use App\Tag;

instead of:

use phpDocumentor\Reflection\DocBlock\Tag;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download