我正在做一個家庭專案,不是一個真正的好程式員,我使用一個已經制作的應用程式來編輯自己,我在我的產品表中添加了多個列作為遷移:
public function up()
{
Schema::table('products', function (Blueprint $table) {
//
$table->integer('N_serie')->after('stock');
$table->integer('quantite_fabrique')->after('N_serie');
$table->unsignedDecimal('prix_qtt_fabrique', 10, 2)->after('quantite_fabrique');
$table->integer('unite_lg')->after('prix_qtt_fabrique')->nullable();
$table->integer('qtt_fabr_par_unit')->after('unite_lg')->nullable();
$table->unsignedDecimal('prix_unite', 10, 2)->after('qtt_fabr_par_unit');
$table->unsignedDecimal('prix_par_kg', 10, 2)->after('prix_unite');
$table->unsignedDecimal('prix_1', 10, 2)->after('prix_par_kg')->nullable();
$table->unsignedDecimal('prix_45', 10, 2)->after('prix_1')->nullable();
$table->unsignedDecimal('prix_total', 10, 2)->after('prix_45')->nullable();
$table->unsignedDecimal('comis_commerciale', 10, 2)->after('prix_total');
$table->unsignedDecimal('prix_gros', 10, 2)->after('comis_commerciale');
});
}
我更改了可填充變數,并將這些附加到舊變數
protected $fillable = [
'name', 'description', 'product_category_id', 'price', 'stock','N_serie', 'quantite_fabrique','prix_qtt_fabrique','unite_lg','qtt_fabr_par_unit','prix_unite','prix_par_kg','prix_1','prix_45','prix_total','comis_commerciale','prix_gros','stock_defective'
];
I went to my view and added the new columns to the form, to be added, this is an example of one column code :
<div class="form-group{{ $errors->has('N_serie') ? ' has-danger' : '' }}">
<label class="form-control-label" for="input-name">N° de Série</label>
<input type="text" name="name" id="input-name" class="form-control form-control-alternative{{ $errors->has('N_serie') ? ' is-invalid' : '' }}" placeholder="N° de Série" value="{{ old('N_serie') }}" required autofocus>
@include('alerts.feedback', ['field' => 'N_serie'])
</div>
當我點擊保存按鈕時,我收到此錯誤:
SQLSTATE[HY000]: General error: 1364 Field 'quantite_fabrique' doesn't have a default value (SQL: insert into products (price, category_id, updated_at, created_at) values (20, 3, 2020-12-11 07:00:34, 2020-12-11 07:00:34))
這是我的控制器功能,可以保存所有內容:
public function store(ProductRequest $request, Product $model)
{
$model->create($request->all());
return redirect()
->route('products.index')
->withStatus('Product successfully registered.');
}
不知道問題出在哪里,求大神幫忙
uj5u.com熱心網友回復:
您已將 quantite_fabrique 列設定為非空。您應該填寫該欄位。由于您使用的是 ProductRequest ,因此您可以使用
public function store(ProductRequest $request)
{
if(Product::create($request->validated())){
return redirect()
->route('products.index')
->withStatus('Product successfully registered.');
}
return redirect()
->route('products.index'); // throw your error here
}
此外,您的 ProductRequest 必須具有“quantite_fabrique”欄位作為驗證欄位。
uj5u.com熱心網友回復:
我的問題解決了,我沒有編輯 ProductRequest 類來接受新添加的列并將它們發送到請求中,這就是為什么在 sql 代碼“插入...”中不接受新的列 -
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/442566.html
上一篇:mysql中的標量函式回傳不一致的值。取決于被呼叫的位置
下一篇:如何優化這個基于范圍的查詢?
