Reputation: 311
Hello i have a problem that i think it's easy for someone but i'm not you're i'm human :) So my problem is this :
SQLSTATE[HY000]: General error: 1364 Field 'quantity' doesn't have a default value (SQL: insert into products (title, price, description, subtitle, token, type_of_product, category, updated_at, created_at) values (bonjour, 20, bonjour, bonjour, MWnBp|z|/#o"/G, 1, hello, 2020-04-03 20:33:38, 2020-04-03 20:33:38))
I have specified the field in my blade file :
<input name="quantity" placeholder="Quantity Of Your Product">
My Controller :
//Create Product In Database with Mysql eloquent
public function pushFormProduct(Request $request){
$inputs = $request->all();
$product = new Product();
$product->title = $inputs['title'];
$product->subtitle = $inputs['subtitle'];
$product->description = $inputs['description'];
$product->quantity = $inputs['quantity'];
$product->price = $inputs['price'];
$product->type_of_product = $inputs['type_of_product'];
$product->category = $inputs['category'];
$product->create($inputs);
return redirect(route('admin.view.product'));
}
And if i :
dd($inputs['quantity'])
That return me the exact quantity of the product.
But if i push all field to create a new products i have the error ( i declared it in the upside :))
So i don't know who, and why i have this bug, because the field have the good value. Any purpose ?
Upvotes: 1
Views: 450
Reputation: 11504
You're doing a couple of things wrong:
class Product extends Model
{
protected $fillable = [
'title',
'subtitle',
'description',
'quantity', // <---
'price',
'type_of_product',
'category',
];
}
You're using create incorrectly. You should call Product::create($request->all())
instead. Or:
Product::create(
$request->only([
'title',
'subtitle',
'description',
'quantity',
'price',
'type_of_product',
'category',
])
);
$product = new Product;
(which allows you to to bypass fillable if you set e.g. $product->quantity = 123;
then you need to call $product->save();
afterwards.Upvotes: 1