我的帖子页面上有一个文本区域输入字段,我正在尝试使用它通过ajax提交用户评论。但是我一直得到SQL错误注释字段不能为空。我很肯定我正在用数据填充该字段。
SQLSTATE[23000]:完整性约束冲突:1048列"注释"不能为空(SQL:插入到
comments
(user_id
,comment
,parent_id
,parents
,updated_at
,created_at
( 值 (1, , , 0, 2015-10-16 19:16:26, 2015-10-16 19:16:26((
做dd(Input::get('commenter_comment))
会给null
我也在控制台中收到此错误
开机自检 http://localhost/reddit/public/posts/post_this_comment 500(内部服务器错误(
这些是我的路线
Route::resource('posts', 'PostsController');
Route::post('posts/post_this_comment', 'PostsController@post_this_comment');
post_this_comment()
方法PostsController
public function post_this_comment(Request $request){
$comment = new Comment;
$comment->user_id = Auth::id();
$comment->comment = Input::get('commenter_comment');
$comment->parent_id = Input::get('commenter_parent');
if($comment->parent_id > 0){
$my_parent = Comment::find($comment->parent_id);
$comment->parents = $my_parent->parents.'.'.$comment->parent_id;
}else{
$comment->parents = '0';
}
$comment->save();
$per_page = Input::get('per_page');
$comment_list = view('eastgate.comment.comment_list')
->with('comments', $this->comment_list($per_page, $request))
->with('total_comments', $this->total_comments())
->with('per_page', $per_page)
->render();
$response = array(
'status' => 'success',
'msg' => 'Comment Saved!',
'comment_list' => $comment_list
);
return Response::json($response);
}
视图
<div class="comment-fields">
<div class="row commenter-comment">
<div class="form-group col-md-12">
<textarea id="commenter_comment" name="commenter_comment" class="form-control comment-field" title="User's comment" placeholder="Comment Text"></textarea>
</div>
</div>
<div class="row commenter-name-email">
<input type="hidden" id="commenter_parent" name="commenter_parent" class="commenter-parent" value="0">
</div>
<div class="row commenter-captcha">
<div class="col-md-3 text-right">
<a href="javascript:void(0)" class="btn btn-info post-this-comment">Post</a>
</div>
</div>
</div>
$(document).on('click', 'a.post-this-comment', function(){
var formData = new FormData();
var arrayelem = commenter_fields();
var elem;
for(var i=0, size = arrayelem.length; i<size; i++){
elem = arrayelem[i];
formData.append(elem, $('#'+elem).val());
}
formData.append('per_page', $('.comments_per_page').val());
var request = $.ajax({ // push question data to server
type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
url : 'post_this_comment', // the url where we want to POST
data : formData, // our data object
dataType : 'json',
processData : false,
contentType : false
});
request.done(comment_done_handler);
request.fail(comment_fail_handler); // fail promise callback
});
试试
$comment->comment = $request->input('commenter_comment');
$comment->parent_id = $request->input('commenter_parent');
最后,问题出在 ajax 请求上。
$(document).on('click', 'a.post-this-comment', function(){
var formData = 'commenter_comment='+$('textarea[name=commenter_comment]').val();
//formData.append('commenter_comment', $('textarea[name=commenter_comment]').val());
console.log(formData);
var request = $.ajax({ // push question data to server
type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
url : 'post_this_comment', // the url where we want to POST
data : formData // our data object
});
request.done(comment_done_handler);
request.fail(comment_fail_handler); // fail promise callback
});
更改为此格式有效。
如果您
使用的是Laravel 5.x,请不要使用Input::get()
,因为版本5及以上建议使用$request->input('something')
您必须使用以下方法:
use Illuminate'Http'Request;
然后,您可以使用:
if($request->has('commenter_comment'))
{
$comment->comment = $request->input('commenter_comment');
}
上面的 if 条件检查输入是否实际发送并具有值。在您尝试使用它之前,这很有用。