title: How can I set the default value Laravel <select> element?
body: <p>I want to retrieve database values in category name and i want to show default value in selection. This is my controller for my edit view.</p>
  
  <p>I have a <code>category_product</code> table.</p>
  
  <p><a href="https://i.stack.imgur.com/CmzB7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CmzB7.jpg" alt="category_product"></a></p>
  
  <pre><code>&lt;div class="col-md-4"&gt;
      &lt;div class="form-group"&gt;
          &lt;label for="category"&gt;category&lt;/label&gt;
          &lt;select class="form-control" name="category" id="category"&gt;
              @foreach($categories as $category)
                  &lt;option value="{{ $category-&gt;id }}" {{ $product-&gt;categories()-&gt;category_id == $category-&gt;id ? 'selected' : '' }}&gt;{{ $category-&gt;name }}&lt;/option&gt;
              @endforeach
          &lt;/select&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>ProductController.php</p>
  
  <pre><code>public function edit(Product $product)
  {
      $categories = Category::all();
      return view('Admin.products.edit', compact('product', 'categories'));
  }
  </code></pre>
  
  <p>Product.php</p>
  
  <pre><code>public function categories()
  {
      return $this-&gt;belongsToMany(Category::class);
  }
  </code></pre>
  
  <p>I get this error.</p>
  
  <blockquote>
    <p>Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$category_id </p>
  </blockquote>