_jbaker_'s avatar

Select2 Tags Edit Post

Hi i created a tag functionality for my posts so when i create a new post i dont have any problem and here is my code

<div style="width:250px; !important;"  class="search__wrapper styled-select color rounded">
<span style="color:white !important;" class="search__item__icon--category ion-pricetag"></span>
<select name="tag_list[]" id="tag_list" class="select2-multi" multiple="multiple">
       @foreach ($tags as $tag)
               <option value="{{$tag->id}}">{{$tag->name}}</option>
       @endforeach
   </select>
 </div>

But the problem is when i edit a post i cant show the selected tags for the specific post here is my code for edit post

<div style="width:250px; !important;"  class="search__wrapper styled-select color rounded">
 <span style="color:white !important;" class="search__item__icon--category ion-pricetag"></span>
  <select name="tag_list[]" id="tag_list" class="select2-multi" multiple="multiple">
         @foreach ($tags as $tag)
              <option value="{{$tag->id}}">{{$tag->name}}</option>
         @endforeach
 </select>
</div>

0 likes
6 replies
_jbaker_'s avatar

I created another row named article_tags because the relationship between the article and tags is belognsToMany so here is the article_tag code

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticleTagTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('article_tag', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('article_id');
            $table->integer('tag_id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('article_tag');
    }
}

satish9323's avatar
Level 2

Okay here are the steps to follow.

  1. in edit function of controller get all the tags of particular article and pass them to view.

  2. In view check in your for loop of select if that id exists in tags array, if it exists use 'selected' attribute.

    @foreach ($tags as $tag)
                  <option value="{{$tag->id}}" {{ in_array($tag->id,$selected_tags)?'selected':''}}> {{$tag->name}}</option>
         @endforeach 
    
3 likes
Rrrr's avatar

im soo late,but what is variable $selected_tags?

anuzpandey's avatar

@rrrr $selected_tags are the tags that a post has. To get $selected_tags:

$selected_tags = array();
foreach ($post->tags as $post_tag) {
     array_push($selected_tags, $post_tag->id);
}

Or the other solution is:

@foreach($tags as $tag)
    <option value="{{ $tag->id }}"
        {{ ($post->tags()->pluck('id')->contains($tag->id)) ? 'selected' : '' }}
    >{{ $tag->id . ' - ' . $tag->name }}</option>
@endforeach
1 like

Please or to participate in this conversation.