I have read or watched many laravel tutorials on the web,most of the tutorials' tagging system have a feature:
The tag CRUD has their own router,first,add new tags and select tags when create new post.But if I want to dynamically add tags or update tags when create post like wordpress, How can I do ? (I'm using bootstrap-tagsinput plugin)
For example:
Route::resource('tags','TagsController');
Tags CRUD all work in this way.
What I want to do is :
Route::resource('posts','PostController');
When I create new post or edit post, I also can add or delete tags without use the Tag Router
For create post I can use laravel's saveMany method like this:
$post = new Post();
$post->someProperty = $someProperty;
$post->save();
$tags = [];
foreach ($request->tags as $tag) {
$tags[] = new Tag(['name' => $tag]);
}
$post->saveMany($tags);
But when I edit a post, I also want to delete tags or add new tags,How can I do for it?
use this tagging package which has all the features you want so you don't have to re-invent the wheel.
So, there you can untag or delete tag or retag completely
$post->untag('Cooking'); // remove Cooking tag
$post->untag(); // remove all tags
$post->retag(array('Fruit', 'Fish'));
you can easily use:
$post->tags()->sync($request->tags, false);
you can review the function sync
documentation under Syncing Associations section and you can see this answer