路线
Route::resource('/post', 'PostController');
控制器
public function store(Request $request)
{
$post = new Post();
$post->PostDate = $request->PostDate[$this->DEFAULT_LANGUAGE] ? date('Y-m-d', strtotime($request->PostDate[$this->DEFAULT_LANGUAGE])) : Carbon::now();
$post->PostTitle = $request->PostTitle[$this->DEFAULT_LANGUAGE];
$post->PostContent = $request->PostContent[$this->DEFAULT_LANGUAGE] ? $request->PostContent[$this->DEFAULT_LANGUAGE] : '';
$post->PostSlug = $request->PostSlug[$this->DEFAULT_LANGUAGE] ? $request->PostSlug[$this->DEFAULT_LANGUAGE] : $post->PostTitle;
$post->PostTypeID = $request->PostTypeID[$this->DEFAULT_LANGUAGE]? $request->PostTypeID[$this->DEFAULT_LANGUAGE] : 1;
$post->LanguageID = $this->DEFAULT_LANGUAGE;
$post->SEODescription = $request->SEODescription[$this->DEFAULT_LANGUAGE] ? $request->SEODescription[$this->DEFAULT_LANGUAGE] : '';
$post->AllowComment = $request->AllowComment[$this->DEFAULT_LANGUAGE] ? $request->AllowComment[$this->DEFAULT_LANGUAGE] : 0;
$post->SidebarLeft = $request->SidebarLeft[$this->DEFAULT_LANGUAGE] ? $request->SidebarLeft[$this->DEFAULT_LANGUAGE] : '';
$post->SidebarRight = $request->SidebarRight[$this->DEFAULT_LANGUAGE] ? $request->SidebarRight[$this->DEFAULT_LANGUAGE] : '';
$post->HTMLBeforeDetail = $request->HTMLBeforeDetail[$this->DEFAULT_LANGUAGE] ? $request->HTMLBeforeDetail[$this->DEFAULT_LANGUAGE] : '';
$post->HTMLAfterDetail = $request->HTMLAfterDetail[$this->DEFAULT_LANGUAGE] ? $request->HTMLAfterDetail[$this->DEFAULT_LANGUAGE] : '';
$post->AllowViewDetail = $request->AllowViewDetail[$this->DEFAULT_LANGUAGE] ? $request->AllowViewDetail[$this->DEFAULT_LANGUAGE] : 0;
$post->AllowViewDetail = $request->PostExpiredDate[$this->DEFAULT_LANGUAGE] ? date('Y-m-d', strtotime($request->PostExpiredDate[$this->DEFAULT_LANGUAGE])) : null;
$post->CustomURL = $request->CustomURL[$this->DEFAULT_LANGUAGE] ? $request->CustomURL[$this->DEFAULT_LANGUAGE] : '';
$post->IsVerified = $request->IsVerified[$this->DEFAULT_LANGUAGE] ? $request->IsVerified[$this->DEFAULT_LANGUAGE] : 0;
$post->FigureCategoryID = $request->FigureCategoryID[$this->DEFAULT_LANGUAGE] ? $request->FigureCategoryID[$this->DEFAULT_LANGUAGE] : null;
$post->FigureCategoryID = $request->ParentID[$this->DEFAULT_LANGUAGE] ? $request->ParentID[$this->DEFAULT_LANGUAGE] : null;
$post->PostedBy = Auth::user()->name;
$post->save();
return response()->json($post);
}
阿贾克斯
$('#post-form').submit(function(){
swal({
title: 'Are you sure?',
type: 'info',
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Confirm!",
closeOnConfirm: false,
closeOnCancel: false
},
//function
function(isConfirm){
if(isConfirm){
$.ajax({
type: "POST",
url: "<?php echo url('post'); ?>",
data: $('#post-form').serialize(),
dataType: 'json',
})
.done(function(data){
if(data.id){
swal({
title: "Saved!",
text: "Your Post has been saved.",
type: "success"}, function(){
console.log(data.status);
});
}else{
swal("Try again");
}
console.log(data);
})
.error(function(data){
swal("Cancelled", "Please fill the data first.");
console.log('Error:', data);
});
} else{
swal("Cancelled");
}
//end function
});
return false;
});
在我的迁移中,我使用
$table->boolean('AllowViewDetail')->default(1);
但是,当我提交数据时,有一个错误:
非空冲突:7错误:“AllowViewDetail”列中的值为空
违反。。
在我的预览中,
AllowViewDetail
的值是1,但是为什么会出现这样的错误? 最佳答案
AllowViewDetail
被分配了两次。
您正在将PostExpiredDate
分配给AllowViewDetail
。
我怀疑您的PostExpiredDate
为空,这是导致错误的原因。
关于php - Laravel使用AJAX来存储数据,但是出错,因为它说数据为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46313134/