I implemented a Many To Many
relation between Posts
and Tags
. I am using Select2 Ajax
for Multi Select
and html is rendered by the Spatie/HTML
package.
In the create form, I can select the tags and while i same the database table updates successfully. I can also show the tags.
I am facing an issue in the edit form. I am using the same create form for the edit and though the Tags has been attached earlier i can not see those as selected. What is the solution of this. Some of the codes are mentioned below.
Form
{{ html()->multiselect('tags_list', '', $posts->tags->pluck('id')->toArray())->class('form-control select2-tags')) }}
Model:
public function tags()
{
return $this->belongsToMany('App\Models\Tag');
}
The following code works, but is there any better solution? may be via Model?
{{ html()->multiselect('tags_list', $posts->tags->pluck('name', 'id'), $posts->tags->pluck('id')->toArray())->class('form-control select2-tags')) }}