mercredi 31 octobre 2018

Delete or Get all the data with specific name tag in laravel

Table 1: products: id, title, publish,created_at
Table 2: tags: id,name,lang,created_at
Table 3: taggable: id,tag_id,taggable_type,taggable_id

In product model:

public function tags()
{
    return $this->morphToMany('App\Tag', 'taggable');
}

I create a new record (in tags table)

$cat = Category::find(1);
$tag = \App\Tag::firstOrCreate(['name' => 'key1']);

Now I save related data in taggables table:

$cat = Category::find(1);
$cat->tags()->save($tag);

It works.

Now I want delete all record for $cat in the taggables:

$cat->tags()->detach();

It works.

Now,in taggables table,I do not have any related data for 'key1' in tags table. So I want delete it.

The Questions:

How can I check that record (name = 'key1') in tags has not any related record in taggable and how can I delete it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire