选择选项(下拉菜单)Laravel

pnwntuvh  于 2022-11-18  发布在  其他
关注(0)|答案(2)|浏览(189)

我做了一个表单的下拉菜单,我将显示下面的代码。但是,当我单击提交按钮时,出现了一个错误消息:
数据库状态[23000]:完整性约束冲突:1048列“brand”不能为空(SQL:插入到supplier_details中。
我从下拉列表中选择的数据实际上为空。实际上,我是Laravel的新用户。
我不想从数据库中创建下拉列表,我只想显示选项,当用户填写完表单后单击提交按钮时,选项将插入到数据库中。

<div class="form-group row">
            <label style="font-size: 16px;" for="id" class = "col-sm-2">Item Brand </label>
            <label for="supp_name" class = "col-sm-1">:</label>
                <div class="col-sm-7">
                    <select name="brand" class="form-control js-example-basic-single" required>
                    <option >Please select item brand</option>  
                    <option value="machine1"> Item Brand 1 </option>  
                    <option value="machine1"> Item Brand 2 </option>  
                    <option value="machine1"> Tem Brand 3 </option>  
                    </select>
                </div>  
         </div>

主计长

public function createsupplierdetails()
        {
            return view ('frontend.praiBarcode.getweight');
        }

    public function supplierdetails(Request $r)
    {

       $details = new SupplierDetail;
       $getuserPO = Supplier::where('PO',$r->PO)->first();

        $details->brand = $getuserPO->brand;
        $details->container_no = $getuserPO->container_no;
        $details->date_received = $getuserPO->date_received;
        $details->gross_weight = $getuserPO->gross_weight;
        $details->tare_weight = $getuserPO->tare_weight;
        $details->net_weight = $getuserPO->net_weight;
        $details->save();

        return view ('frontend.praiBarcode.viewsupplierdetails')
        ->with('details',$details);
        
        }
zz2j4svz

zz2j4svz1#

检查以下内容以验证其是否正常工作:
1.请确保您提交的表单正确。
1.尝试在控制器上执行dddd($request->all())
1.如果数据到达控制器但未插入数据库,请检查您的模型,它是否已添加到fillable,或者受保护的阵列中是否只有id。https://laravel.com/docs/9.x/eloquent#mass-assignment
错误应在修复后立即修复。

jtjikinw

jtjikinw2#

控制器使用验证器;

public function store(Request $request)

  {
    $validator = Validator::make($request->all(), [

        'brand' => 'required',
        
    ]);

    if ($validator->fails()) {
        return redirect()->back()->with('error', $validator->errors()->first());
    }
    $details = new SupplierDetail();

    $details->brand = $request->brand;
    $details->container_no = $request->container_no;
    $details->date_received = $request->date_received;
    $details->gross_weight = $request->gross_weight;
    $details->tare_weight = $request->tare_weight;
    $details->net_weight = $request->net_weight;
    $details->save();
    if ($trending) {
        return redirect(route('details.index'))->with('success', 'Field added successfully');
    } else {
        return redirect()->back()->with('error', 'Field has been not added successfully');
    }
}

相关问题