0
       $product = new Product([
        'productName' => $request->productName,
        'quantity' => $request->quantity,
        'weight' => $request->weight,
        'boxes' => $request->boxes,
        'MRP' => $request->MRP,
        'costprice' =>$request->costprice,
        'productDescription' =>$request->productDescription,
        'seller_id' => $request->user('seller')->id,
        'category_id' => $request->category
    ]);

      $product->save();

I have provided values to the product table but still it is showing this error SQLSTATE[HY000]: General error: 1364 Field 'seller_id' doesn't have a default value (SQL: insert into products (updated_at, created_at) values (2021-12-30 13:35:47, 2021-12-30 13:35:47))

Ritik Rai
  • 11
  • 3
  • Can you dump `$request->user('seller')`? Also, it seems it's only inserting the automatic created/updated dates, and during that insertion is when it's complaining about the lack of a default for `seller_id`. This implies that `$product` is not taking on all those properties you've passed into the constructor. You could try to set those properties instead outside the constructor, like `$product->boxes = $request->boxes;` etc. – Base Desire Dec 30 '21 at 13:52
  • seems you are missing fields in `$fillable` on the model – lagbox Dec 30 '21 at 16:08

1 Answers1

0

Try this instead:

  $product = Product::create([
    'productName' => $request->productName,
    'quantity' => $request->quantity,
    'weight' => $request->weight,
    'boxes' => $request->boxes,
    'MRP' => $request->MRP,
    'costprice' =>$request->costprice,
    'productDescription' =>$request->productDescription,
    'seller_id' => $request->user('seller')->id,
    'category_id' => $request->category
]);

$product should be saved now. Assuming your $request->user('seller')->id actually has data, and assuming your attributes you are filling out are all fillable. mass assignment docs

Base Desire
  • 485
  • 1
  • 5
  • 15