SQLSTATE [23000]:违反完整性约束:1048列'patente'不能为空(SQL:插入cars
(patente
,marca
,modelo
,{ {1}},color
,fecha_ingreso
,updated_at
)值(?,?,?,?,?,2019-06-10 16:27:35,2019-06- 10 16:27:35)
created_at
要形成的短代码
Route::match(['get', 'post'], '/crear',[
'uses'=>'CarController@crear',
'as'=>'cars.crear'
]);
代码创建并显示
公共函数crear(Request $ request){
<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>
答案 0 :(得分:0)
在存储数据之前先对其进行验证,
php artisan make:request ClearRequest
在App \ Requests \ ClearRequest中,
class ClearRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'patente'=>'required',
'marca'=>'required',
'modelo'=>'required',
'color'=>'required',
'fecha_ingreso'=>'required',
'patente'=>'required',
'marca'=>'required',
'modelo'=>'required',
'color'=>'required',
'fecha_ingreso'=>'required'
];
}
}
在您的控制器中
use App\Http\Requests\ClearRequest;
以清晰的方式
public function crear(ClearRequest $request){
...
}
在您的视图文件中这样
<form action="/clear" method="POST">
@csrf
patente<br>
<input type="text" name="patente">
<br>
marca<br>
<input type="text" name="marca">
<br>
modelo<br>
<input type="text" name="modelo">
<br>
fecha_ingreso<br>
<input type="text" name="fecha_ingreso">
<br>
patente<br>
<input type="text" name="patente">
<br>
modelo<br>
<input type="text" name="modelo">
<br>
color<br>
<input type="text" name="color">
<br>
fecha_ingreso<br>
<input type="text" name="fecha_ingreso">
<br>
<input type="submit" value="Submit">
</form>
如果这对您有帮助,您支持我:)
答案 1 :(得分:0)
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
数组中。
答案 2 :(得分:-1)
您正在尝试在NOT NULL
列中存储空值。确保您在请求中正确传递了patente
。