title: How to set selected values of Select2 while using AJAX Data with laravel?
body: <p><strong>Controller@Update</strong></p>
  
  <pre><code>  //Update
    $course = Course::findOrFail($id);
    $course-&gt;name = Input::get('name');
    $course-&gt;code = Input::get('code');
    $course-&gt;credits = Input::get('credits');
    $course-&gt;description = Input::get('description');
  
    $course-&gt;tags()-&gt;sync(Input::get('tags')); // Use of sync method
  
    //return [$course];
    $course-&gt;save();
    //Redirect
    Session::flash('message', 'Successfully edited the course : '.$course-&gt;name);
    return $this-&gt;show($course-&gt;code);
  </code></pre>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;pre&gt;
  &lt;div class="form-group"&gt;
       {{Form::label('tags', 'Tag')}}
       {{Form::select('tags[]',[],null,array('multiple'=&gt;'multiple','name'=&gt;'tags[]','id'=&gt;'tag_list','class'=&gt;'form-control'))}}
     &lt;/div&gt;
  &lt;/pre&gt;
  </code></pre>
  
  <p><strong>Select2 script</strong></p>
  
  <pre><code>function tagResultTemplater(tag) {
      return tag.name + " : " + tag.type;
    }
  
    function tagSelectionTemplater(tag) {
      return tag.id + " "+tag.name + " : " + tag.type;
    }
    $("#tag_list").select2({
      ajax: {
        url: "{!! route('tags.json') !!}",
        dataType: 'json',
        delay: 250,
        tags: true,
        data: function (params) {
          return {
            q: params.term, // search term
          };
        },
        processResults: function (data) {
          return {
            results: data
          };
        },
        cache: true
      },
      minimumInputLength: 1,
      placeholder: function(){
        $(this).data('placeholder');
      },
      templateResult: tagResultTemplater,
      templateSelection: tagSelectionTemplater
  
    });
  </code></pre>
  
  <p>I'm working on course management system as a grad project, in short there is a many to many relation between courses and tags , and I wanted to use the sync method with the Select2 package to attach and deattach tags from courses in a convenient way but I can not find a way to set the selected values of the Select2 while using ajax data. If anyone can provide a simple guide guide on how to do this it would be really appreciated :D.</p>