php - 问题 Integrity constraint violation : 1048 Column

标签 php html css laravel

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'patente' cannot be null (SQL: insert into cars (patente, marca, modelo, color, fecha_ingreso, updated_at, created_at) values (?, ?, ?, ?, ?, 2019-06-10 16:27:35, 2019-06-10 16:27:35)

Route::match(['get', 'post'], '/crear',[
    'uses'=>'CarController@crear',
    'as'=>'cars.crear' 
]);

形成短代码

<div class="row">

        <div class="col-md-6"></div>

            <form action="{{route('cars.crear')}}" method="post">

               @csrf

               <div class="row form-group">

                    <div class="col-md-12">

                        <label for="true">Patente:</label>

                        <input type="text" name="patente" size="6" maxlength="6" class="form-control"  required>

                    </div>

                </div>

代码创建和显示

公共(public)函数 crear(请求 $request){

    $patente=$request['patente'];

    $marca=$request['marca'];

    $modelo=$request['modelo'];

    $color=$request['color'];

    $fecha_ingreso=$request['fecha_ingreso'];
   
    $car=new Car();

    $car->patente=$patente;

    $car->marca=$marca;

    $car->modelo=$modelo;

    $car->color=$color;

    $car->fecha_ingreso=$fecha_ingreso;

    $car->save();
   

    return redirect()->back();

}

public function show(){

    $cars=Car::all();

    return view ('lista',['cars'=>$cars]);

}

最佳答案

CarController.php

public function crear(Request $request){
    request()->validate([
        'patente' => 'required',
        'marca' => 'required',
        'modelo' => 'required',
        'color' => 'required',
        'fecha_ingreso' => 'required',
        'patente' => 'required',
        'marca' => 'required',
        'modelo' => 'required',
        'color' => 'required',
        'fecha_ingreso' => 'required'
    ]);

    $car = Car::create([
        patente => $request->patente,
        marca => $request->marca,
        modelo => $request->modelo,
        color => $request->color,
        fecha_ingreso => $request->fecha_ingreso
    ]);

    return redirect()->back();
}

您的代码看起来不错,但有点冗长,所以我稍微整理了一下并添加了验证。我能想到的唯一建议是确保您的 Car 模型将字段添加到 protected $fillable 数组。

关于php - 问题 Integrity constraint violation : 1048 Column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56530256/

相关文章:

html - CSS 图像在 while 循环中重叠

javascript - 使用 background-size : cover; 计算背景集上特定部分的大小

php - Windows 上的 PHP CLI 选项

php - 发送 APNS 命令 = 2

Php Json 获取多个对象

javascript - 浏览器控制台错误

php - 查询 : return all rows even if the count of the associated rows are 0

html - 如何在 HTML Canvas 中绘制渐变线?

html - 当我更改/First Website 下面的 div 的位置时,标题会移动

html - 如何创建不同级别的粘性标题?