Php SQLSTATE[23000]:完整性约束冲突:1048列';说明';在laravel中不能为null(SQL:insert into)
我正试图在我的数据库中的“posts”列中添加一条新注释,但是我收到以下错误[QueryException”,“message:“SQLSTATE[23000]:完整性约束冲突:1048列“description”不能为空 虽然我在表格中填写了描述字段Php SQLSTATE[23000]:完整性约束冲突:1048列';说明';在laravel中不能为null(SQL:insert into),php,jquery,mysql,ajax,laravel,Php,Jquery,Mysql,Ajax,Laravel,我正试图在我的数据库中的“posts”列中添加一条新注释,但是我收到以下错误[QueryException”,“message:“SQLSTATE[23000]:完整性约束冲突:1048列“description”不能为空 虽然我在表格中填写了描述字段 ajax: $('#frcoments').on('submit',function (e) { e.preventDefault(); var description = $('#descri
ajax:
$('#frcoments').on('submit',function (e) {
e.preventDefault();
var description = $('#description').val();
var da = new Date();
var dat = da.toLocaleDateString();
var ti = new Date();
var tim = ti.toLocaleTimeString();
var dat_tim = dat.concat(" ",tim);
$.ajax({
type: 'post',
dataType: 'json',
data: {description: description},
success: function( data ) {
info.hide().find('ul').empty();
if(!data.errors){
$("#respod").append('<div class="fils-body"><p style="margin-bottom:3px; font-size:12px">'+data['description']+'</p></div>');
document.getElementById("description").value="";
$("#description").focus();
}else{
$.each(data.errors, function(index, error) {
info.find('ul').append('<li>'+error+'</li>');
});
info.slideDown();
$("#description").focus();
}
},
error:function(){}
});
// }
});
view:
{!! Form::open(array('url'=> $jour->users_id.'/journal', 'method'=>'POST', 'id'=>'frcoments')) !!}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
{!! Form::text('description', null, ['class' =>'form-control', 'style'=>'border-radius: 0']) !!}
</div>
<div class="form-group">
{!! Form::submit('Publier', array('class'=>'btn btn-danger')) !!}
</div>
{!! Form::close() !!}
controller:
if($request->ajax()){
$coment=Input::get('description');
$Coments= new \App\Post;
$Coments->journals_id = '5';
$Coments->date = \Carbon\Carbon::now();
$Coments->aimer='0';
$Coments->naimer='0';
$Coments->description=$coment;
$Coments->save();
return \Response::json($Coments);
}else{
return 'no';
}
ajax:
$(“#frcoments”)。关于('submit',函数(e){
e、 预防默认值();
var description=$('#description').val();
var da=新日期();
var dat=da.toLocaleDateString();
var ti=新日期();
var tim=ti.toLocaleTimeString();
var dat_tim=dat.concat(“,tim”);
$.ajax({
键入:“post”,
数据类型:“json”,
数据:{description:description},
成功:功能(数据){
info.hide().find('ul').empty();
如果(!data.errors){
$(“#respod”).append(“”+数据['description']+”
”);
document.getElementById(“说明”).value=“”;
$(“#说明”).focus();
}否则{
$.each(数据错误、函数(索引、错误){
info.find('ul').append(''+error+' ');
});
info.slideDown();
$(“#说明”).focus();
}
},
错误:函数(){}
});
// }
});
视图:
{!!Form::open(数组('url'=>$jour->users\u id.'/journal','method'=>'POST','id'=>'frcoments'))
{!!Form::text('description',null,['class'=>'Form-control','style'=>'border-radius:0'])
{!!Form::submit('Publier',array('class'=>'btn-btn-danger'))
{!!Form::close()!!}
控制器:
如果($request->ajax()){
$coment=Input::get('description');
$Coments=new\App\Post;
$Coments->journals_id='5';
$Coments->date=\Carbon\Carbon::now();
$Coments->aimer='0';
$Coments->naimer='0';
$Coments->description=$Coments;
$Coments->save();
return\Response::json($Coments);
}否则{
返回“否”;
}
您试图在“名称”字段中插入空值,但尚未将此列定义为可空。
尝试在迁移中使用“名称”定义中的
->nullable()
方法。查看@Jesus Amiero提到的此链接
您正试图在“名称”字段中插入空值,但是 尚未将此列定义为可空。请尝试使用->可空() 方法在迁移中的“名称”定义中
但是,如果您不想在迁移中弄脏手脚,只需转到
description
列所在的表,并在那里插入一个相关的值即可。您不是像文档那样在ajax中将description
设置为null
。getElementById(“description”).value=“”;
?是的,我在保存到数据库中后立即清空。您如何才能从javascript代码保存到数据库?这不是控制器的工作吗?即使我删除了文档行。getElementById(“description”).value=“”;错误仍然存在!?我没有名称字段!?如何使迁移中的名称字段可为null?请查看您的数据库,因为错误是描述性的:“列'name'不能为null”。如果您的表中有此字段,请修改它并将其设置为可空。我在数据库中没有名称字段!很抱歉,我在标题中犯了一个错误,我指的是“说明”字段。如何使迁移中的名称字段可为空?转到您的Laravel项目>>数据库目录>>迁移>>在描述的位置查找迁移离子字段是预设的。将->nullable()
附加到描述中,它看起来像描述->nullable();
然后从命令行运行php artisan migrate:refresh
。当我执行php artisan migrate:refresh时,我会收到以下消息:[Illumb\Database\QueryException]SQLSTATE[42S02]:找不到基表或视图:1051未知表'db_metie rs.posts'(SQL:drop tableposts
)当我使用description->nullable()时,错误消失;但在这种情况下,我无法填充description列!!!在description字段中插入相关值。您不能填充是什么意思?能否从PHPmyadmin在其中插入一个值