Laravel选择字段不能为空

时间:2017-06-04 13:19:24

标签: php laravel

我试图将表单存储到我的数据库中,但这是我得到的错误:

Integrity constraint violation: 1048 Column 'rent_product' cannot be null 

为什么会这样?

我试图在数组中执行此操作,因为我可以选择显示更多选择字段(添加更多产品以供租借)。

这是HTML:

<div class="col-lg-10">
 <label for="sel1">Selecteer product:</label>
   <select class="form-control" id="sel1" style="height: 40px;">
     @foreach ($pr as $prr)
      <option name="product_name[]" value="{{$prr->product_name}}">{{$prr->product_name}}</option>
     @endforeach
   </select>
</div>

功能:

public function create_order($id) {

    $customer = Customer::find($id);

    $customer->rent_product = Request::get('product_name');
    $customer->rent_product_count = Request::get('product_count');
    $customer->update();


}

提前致谢。

3 个答案:

答案 0 :(得分:1)

我认为你应该在select标签中使用name属性。代码

<div class="col-lg-10">
 <label for="sel1">Selecteer product:</label>
   <select name="product_name" class="form-control" id="sel1" style="height: 40px;">
     @foreach ($pr as $prr)
      <option value="{{$prr->product_name}}">{{$prr->product_name}}</option>
     @endforeach
   </select>
</div>

然后

$customer = Customer::find($id);
$customer->rent_product = Request::get('product_name');
$customer->rent_product_count = Request::get('product_count');
$customer->save();

答案 1 :(得分:0)

$customer->update();更改为$customer->save();update方法要求您传递属性数组以进行更新。所以你在没有任何价值的情况下进行更新。

$customer = Customer::find($id);
$customer->rent_product = Request::get('product_name');
$customer->rent_product_count = Request::get('product_count');
$customer->save();

// This is what you need to do if you want to use `update`

$customer->update([
    'rent_product' => Request::get('product_name'),
    'rent_product_count' => Request::get('product_count')
]);

// Or you can do this to update in a single query

Customer::where('id', $id)->update([
    'rent_product' => Request::get('product_name'),
    'rent_product_count' => Request::get('product_count')
]);

答案 2 :(得分:-2)

错误表明您的模型没有名为rent_product的列。如果您已设置此列,请检查Customer迁移。

这样的事情:

// file: database/migrations/20170000_create_customers_table.php

Schema::create("customers", function (Blueprint $table) {
    $table->increments('id');
    $table->string('rent_product');
    $table->integer('rent_product_count');
    $table->timestamps();
});
您的客户表中

$customer->rent_productrent_product。 (在数据库中)

另请检查您是否已使用php artisan migrate迁移了表格。