title: laravel-5.7: SQLSTATE[HY000]: General error: 1364 Field 'category_id' doesn't have a default value
body: <p>I'm new to Laravel and trying to add <code>Product</code> under <code>Category</code> but when I add <code>Product</code> then it shows this error: </p>
  
  <blockquote>
    <p>SQLSTATE[HY000]: General error: 1364 Field 'category_id' doesn't have a default value (SQL: insert into <code>products</code> ..."</p>
  </blockquote>
  
  <p>Initially i was adding these products without under any category than it was working and now its not adding under <code>Category</code>.</p>
  
  <p>can anyone would prefer to provide me its solution?</p>
  
  <p>here is my form:</p>
  
  <pre class="lang-html prettyprint-override"><code>  &lt;form enctype="multipart/form-data" class="form-horizontal" method="post" action="{{ url('admin/add-product') }}" name="add_product" id="add_product" novalidate="novalidate"&gt;{{ csrf_field() }}
            &lt;div class="control-group"&gt;
              &lt;label class="control-label"&gt;Under Category&lt;/label&gt;
              &lt;div class="controls"&gt;
                &lt;select name="category_id" id="category_id" style="width:220px;"&gt;
                  &lt;?php echo $categories_drop_down; ?&gt;
                &lt;/select&gt;
              &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class="control-group"&gt;
              &lt;label class="control-label"&gt;Product Name&lt;/label&gt;
              &lt;div class="controls"&gt;
                &lt;input type="text" name="product_name" id="product_name"&gt;
              &lt;/div&gt;
            &lt;/div&gt;
    &lt;div class="uploader" id="uniform-undefined"&gt;&lt;input name="image" id="image" type="file" size="19" style="opacity: 0;"&gt;&lt;span class="filename"&gt;No file selected&lt;/span&gt;&lt;span class="action"&gt;Choose File&lt;/span&gt;&lt;/div&gt;
    div class="form-actions"&gt;
              &lt;input type="submit" value="Add Product" class="btn btn-success"&gt;
            &lt;/div&gt;
          &lt;/form&gt;
  </code></pre>
  
  <p>here is <code>ProductsController</code>:</p>
  
  <p><strong>ProductsController.php</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>public function addProduct(Request $request)
  {
      if ($request-&gt;isMethod('post'))
      {
          $data = $request-&gt;all();
  
          $product = new Product;
          $product-&gt;product_name = $data['product_name'];
          $product-&gt;product_code = $data['product_code'];
          $product-&gt;product_color = $data['product_color'];
          if ( ! empty($data['description']))
          {
              $product-&gt;description = $data['description'];
          }
          else
          {
              $product-&gt;description = '';
          }
          $product-&gt;price = $data['price'];
  
          // Upload Image
          if ($request-&gt;hasFile('image'))
          {
              $image_tmp = Input::file('image');
              if ($image_tmp-&gt;isValid())
              {
                  $extension = $image_tmp-&gt;getClientOriginalExtension();
                  $filename = rand(111, 99999) . '.' . $extension;
                  $large_image_path = 'images/backend_images/products/large/' . $filename;
                  $medium_image_path = 'images/backend_images/products/medium/' . $filename;
                  $small_image_path = 'images/backend_images/products/small/' . $filename;
                  // Resize Images
                  Image::make($image_tmp)-&gt;save($large_image_path);
                  Image::make($image_tmp)-&gt;resize(600, 600)-&gt;save($medium_image_path);
                  Image::make($image_tmp)-&gt;resize(300, 300)-&gt;save($small_image_path);
                  // Store image name in products table
                  $product-&gt;image = $filename;
              }
          }
  
          $product-&gt;save();
  
          /*return redirect()-&gt;back()-&gt;with('flash_message_success','Product has been added successfully!');*/
  
          return redirect('/admin/view-products')-&gt;with('flash_message_success', 'Product has been added successfully!');
      }
  
      $categories = Category::where(['parent_id' =&gt; 0])-&gt;get();
      $categories_drop_down = "&lt;option value='' selected disabled&gt;Select&lt;/option&gt;";
  
      foreach ($categories as $cat)
      {
          $categories_drop_down .= "&lt;option value='" . $cat-&gt;id . "'&gt;" . $cat-&gt;name . "&lt;/option&gt;";
          $sub_categories = Category::where(['parent_id' =&gt; $cat-&gt;id])-&gt;get();
          foreach ($sub_categories as $sub_cat)
          {
              $categories_drop_down .= "&lt;option value='" . $sub_cat-&gt;id . "'&gt;&amp;nbsp;&amp;nbsp;--&amp;nbsp;" . $sub_cat-&gt;name . "&lt;/option&gt;";
          }
      }
  
      return view('admin.products.add_product')-&gt;with(compact('categories_drop_down'));
  }
  </code></pre>