php - SQLSTATE [23000]:违反完整性约束:1048 列 'cne' 不能为空
问题描述
保存数据时出现错误。
Illuminate\Database\QueryException
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'cne' cannot be null (SQL: insert into `students` (`cne`, `firstName`, `secondName`, `age`, `speciality`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, 2020-04-03 20:47:34, 2020-04-03 20:47:34))
我想知道我能做些什么来解决这个问题。
路线
Route::get('/',"StudentController@index") ;
Route::get('/edit/{id}',"StudentController@edit") ;
Route::get('/show/{id}',"StudentController@show") ;
Route::get('/create',"StudentController@create") ;
Route::get('/store',"StudentController@store") ;
Route::get('/update/{id}',"StudentController@update") ;
控制器
public function store(Request $request )
{
$student = new Student() ;
$student->cne = $request->input('cne') ;
$student->firstName = $request->input('firstName') ;
$student->secondName = $request->input('secondName') ;
$student->age = $request->input('age') ;
$student->speciality = $request->input('speciality') ;
$student->save() ;
return redirect('/') ;
//
}
HTML
@if($layout == 'index')
<div class="container-fluid">
<div class="row">
<selctio class="col">
@include("studentslist")
</selction>
<selction class="col"></selction>
</div>
</div>
@elseif($layout == 'create')
<div class="container-fluid" id="create-form">
<div class="row">
<selctio class="col">
@include("studentslist")
</selction>
<selction class="col">
<form action="{{ url('/store') }}" method="POST" >
@csrf
<div class="form-group">
<label>CNE</label>
<input name="cne" id="cne" type="text" class="form-control" placeholder="Enter cne">
</div>
<div class="form-group">
<label>First Name</label>
<input name="firstName" id="firstName" type="text" class="form-control" placeholder="Enter the first name">
</div>
<div class="form-group">
<label>second Name</label>
<input name="secondName" type="text" class="form-control" placeholder="Enter second name">
</div>
<div class="form-group">
<label>Age</label>
<input name="age" type="text" class="form-control" placeholder="Enter the Age">
</div>
<div class="form-group">
<label>Speciality</label>
<input name="speciality" type="text" class="form-control" placeholder="Enter Sepeciality">
</div>
<input type="submit" class="btn btn-info" value="Save">
<input type="reset" class="btn btn-warning" value="Reset">
</form>
</selction>
</div>
</div>
@elseif($layout == 'show')
<div class="container-fluid">
<div class="row">
<selctio class="col">
@include("studentslist")
</selction>
<selction class="col"></selction>
</div>
</div>
@elseif($layout == 'edit')
<div class="container-fluid">
<div class="row">
<section class="col-md-7">
@include("studentslist")
</section>
<section class="col-md-5">
</section>
</div>
</div>
@endif
解决方案
当列..不能为空时,会触发“1048 列”不能为空”错误。
你应该看看你的迁移文件。像这样添加可为空的方法:
$table->string('cne')->nullable();
如果您不想重写迁移,则需要使用类似的东西:
$table->string('cne')->nullable()->change();
此外,更改 Route 文件,以便 store 路由使用 post 方法:
Route::post('/store',"StudentController@store") ;
推荐阅读
- javascript - 我不能使用我添加的表单
- ios - 如何在单元格中添加呼叫按钮
- c++ - 必须进行哪些更改才能使此 Windows 文本框从 2011 年开始在 2019 年正常工作?
- python-3.x - 查找达到以秒为单位的定义年龄的日期
- java - AspectJ 建议未执行
- laravel - 日期和时间戳格式和唯一规则
- java - 使用网络代理 (nanoHTTPD) 即时流式传输 [随机访问] 加密 (AES-CTR) 视频
- unity3d - 粒子系统在一次射击后不会停止
- java - 我需要一个程序,它会要求用户在文件中逐行输入要保存的信息。我该怎么做?
- shopify - 从产品描述中删除图像