gpt4 book ai didi

php - Laravel 使用 AJAX 存储数据,但是因为它说数据为空而出错

转载 作者:行者123 更新时间:2023-11-29 13:47:14 26 4
gpt4 key购买 nike

路线

Route::resource('/post', 'PostController');

Controller

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);
}

Ajax

$('#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);

但是,当我提交数据时,出现错误:

Not null violation: 7 ERROR: null value in column "AllowViewDetail" violates..

在我的预览中,AllowViewDetail 的值为 1,但为什么会出现这样的错误?

最佳答案

AllowViewDetail 被分配了两次。

您正在将 PostExpiredDate 重新分配给 AllowViewDetail

我怀疑您的 PostExpiredDate 为 null,这是导致错误的原因。

关于php - Laravel 使用 AJAX 存储数据,但是因为它说数据为空而出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46313134/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com