title: How to display selected tags in select option value in laravel?
body: <p>This is my Post Create View</p>
  
  <pre><code>&lt;div class="col-lg-12"&gt;
      &lt;form action="{{ route('admin.post.store') }}" enctype="multipart/form-data" method="post"&gt;
          {{ csrf_field() }}
          &lt;div class="form-group"&gt;
              &lt;label for="title"&gt;Post Title&lt;/label&gt;
              &lt;input type="text" class="form-control" value="{{ old('title') }}" name="title" id="title"
                     placeholder="Enter Post Title"&gt;
              &lt;span class="text-danger"&gt;{{ $errors-&gt;first('title') }}&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label for="slug"&gt;Post Image&lt;/label&gt;
              &lt;input type="file" class="form-control" name="image" id="image"
                     placeholder="Select Post Image"&gt;
              &lt;span class="text-danger"&gt;{{ $errors-&gt;first('image') }}&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label for="tags"&gt;Select Tags&lt;/label&gt;
              &lt;select multiple class="form-control" name="tags[]" id="tags"&gt;
                  @foreach($tags as $id =&gt; $name)
                      &lt;option id="{{ $id }}"&gt;{{ $name }}&lt;/option&gt;
                  @endforeach
              &lt;/select&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;textarea class="body" name="body"&gt;{{ old('body') }}&lt;/textarea&gt;
              &lt;span class="text-danger"&gt;{{ $errors-&gt;first('body') }}&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label for="category"&gt;Select Category&lt;/label&gt;
              &lt;select class="form-control" name="category" id="category"&gt;
                  &lt;option value=""&gt;Select&lt;/option&gt;
                  @foreach($cats as $cat)
                      &lt;option value="{{ $cat-&gt;id }}"&gt;{{ $cat-&gt;name }}&lt;/option&gt;
                  @endforeach
              &lt;/select&gt;
          &lt;/div&gt;
          &lt;button type="submit" class="btn btn-success btn-block"&gt;Publish&lt;/button&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Tags have many to many relationship.
  Here I can select many tags, but in the post edit view I cant see the selected tags that selected by me in the post create view.
  I want to show selected tags in select option value and edit theme.</p>
  
  <p>post update methods:</p>
  
  <pre><code>public function edit(Post $post)
  {
      $tags = Tag::all()-&gt;pluck('name', 'id');
      $cats = Category::all();
      return view('admin.post.edit', compact(['post', 'cats', 'tags']));
  }
  
  public function update(Post $post, Request $request)
  {
      $this-&gt;validate($request, [
          'title' =&gt; 'required|min:3|max:255',
          'slug' =&gt; 'nullable|string',
          'image' =&gt; 'sometimes|mimes:jpeg,bmp,png,jpg,gif',
          'body' =&gt; 'required',
          'category' =&gt; 'nullable',
          'views' =&gt; 'nullable',
          'tags' =&gt; 'nullable',
      ]);
  
      $post-&gt;title = $request-&gt;title;
      $post-&gt;slug = $request-&gt;slug;
      $post-&gt;body = $request-&gt;body;
      $post-&gt;category_id = $request-&gt;category;
  
      if ($request-&gt;hasFile('image')) {
          $image = $request-&gt;file('image');
          $filename = time() . '.' . $image-&gt;getClientOriginalExtension();
          $location = public_path('images/' . $filename);
          Image::make($image)-&gt;resize(800, 400)-&gt;save($location);
  
          $oldfilename = $post-&gt;image;
  
          $post-&gt;image = $filename;
  
          Storage::delete($oldfilename);
      }
  
      $post-&gt;save();
  
      $post-&gt;tags()-&gt;sync($request-&gt;tags, false);
  
      Session::flash('update', 'Post Updated Successfully');
  
      return redirect()-&gt;route('admin.post.index');
  }
  </code></pre>
  
  <p>post edit view:</p>
  
  <pre><code>&lt;div class="col-lg-12"&gt;
      &lt;form action="{{ route('admin.post.update',$post-&gt;id) }}" enctype="multipart/form-data" method="post"&gt;
          {{ csrf_field() }}
          {{ method_field('patch') }}
          &lt;div class="form-group"&gt;
              &lt;label for="title"&gt;Post Title&lt;/label&gt;
              &lt;input type="text" class="form-control" value="{{ $post-&gt;title }}" name="title" id="title"
                     placeholder="Enter Post Title"&gt;
              &lt;span class="text-danger"&gt;{{ $errors-&gt;first('title') }}&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label for="slug"&gt;Post Image&lt;/label&gt;
              &lt;input type="file" class="form-control" name="image" id="image"
                     placeholder="Select Post Image"&gt;
              &lt;span class="text-danger"&gt;{{ $errors-&gt;first('image') }}&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label for="tags"&gt;Select Tags&lt;/label&gt;
              &lt;select class="js-example-basic-single form-control" name="tags[]" id="tags" multiple="multiple"&gt;
  
              &lt;/select&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;textarea class="body" name="body"&gt;{{ $post-&gt;body }}&lt;/textarea&gt;
              &lt;span class="text-danger"&gt;{{ $errors-&gt;first('body') }}&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label for="category"&gt;Select Category&lt;/label&gt;
              &lt;select class="form-control" name="category" id="category"&gt;
                  &lt;option value=""&gt;Select&lt;/option&gt;
                  @foreach($cats as $cat)
                      &lt;option &lt;?php if ($cat-&gt;id == $post-&gt;category_id) {
                          echo 'selected';
                      } ?&gt; value="{{ $cat-&gt;id }}"&gt;{{ $cat-&gt;name }}&lt;/option&gt;
                  @endforeach
              &lt;/select&gt;
          &lt;/div&gt;
          &lt;button type="submit" class="btn btn-primary btn-block"&gt;Edit&lt;/button&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>here:</p>
  
  <pre><code>&lt;div class="form-group"&gt;
      &lt;label for="tags"&gt;Select Tags&lt;/label&gt;
      &lt;select multiple class="form-control" name="tags[]" id="tags"&gt;
          @foreach($tags as $id =&gt; $name)
              &lt;option id="{{ $id }}"&gt;{{ in_array($id,$post-&gt;category()-&gt;pluck('id')-&gt;toArray()) ? 'selected' : '' }}&lt;/option&gt;
          @endforeach
      &lt;/select&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>how can I pass selected post tags from database and show theme here and edit them.</p>
  
  <p>I am using select2 plugin</p>
  
  <p>In select2 I should pass data in jQuery:</p>
  
  <pre><code>$('.js-example-basic-single').select2().val({{ json_decode($post-&gt;tags()-&gt;getRelatedIds()) }}).trigger('change');
  </code></pre>
  
  <p>but it doesnt work :(</p>
  
  <p>and I get this error</p>
  
  <blockquote>
    <p>Method Illuminate\Database\Query\Builder::getRelatedIds does not exist. (View: C:\Users\M0RT3Z4\Desktop\MyBlog\resources\views\admin\post\edit.blade.php</p>
  </blockquote>
  
  <p>Please help me, Thanks!</p>