title: laravel 5.4 select2 multiple edit
body: <p>I don't know how to populate the edit function my select2 form. Here's my attempt on doing it. Controller:</p>
  
  <pre><code>public function edit($id)
  {
      $company = Company::find($id);      
  
      $categories = MainCategory::all();
      $categories2 = array();
          foreach ($categories as $category) {
          $categories2[$category-&gt;main_cat_id] = $category-&gt;main_cat_name;
      }
  
      return view('company/edit_company')-&gt;with('company', $company)-&gt;with('categories', $categories2);
  }
  </code></pre>
  
  <p>here's my view:</p>
  
  <pre><code>&lt;select class="form-control select2-multi" name="company_main_cat[]" multiple="multiple" style="width: 50%" data-placeholder="select main category"&gt;
  &lt;option value='{{$category-&gt;main_cat_id}}'&gt;{{$category-&gt;main_cat_name}}&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>here's my JS:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $('.select2-multi').select2();
  $('.select2-multi').select2().val({!! json_encode($company-&gt;maincategories()-&gt;allRelatedIds()) !!}).trigger('change');
  &lt;/script&gt;
  </code></pre>