我在我的帖子頁面上有一個文本區域輸入字段,我試圖用它來通過ajax提交用戶註釋。但我不斷收到SQL錯誤註釋字段不能爲空。我確信我正在填充數據。Laravel 5:無法從視圖中檢索用戶輸入
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
我也得到這個錯誤在控制檯
POST http://localhost/reddit/public/posts/post_this_comment 500(內部服務器錯誤)
這是我的路由
Route::resource('posts', 'PostsController');
Route::post('posts/post_this_comment', '[email protected]_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
});
沒有工作。我得到'未定義的屬性:Illuminate \ Support \ Facades \ Request :: $ commenter_comment' – Halnex
對不起,我的壞。我編輯了我的答案。嘗試這個。如果這不起作用。在你的post_this_comment裏面寫dd($ request); 。通過這個,你將能夠看到所有的接收服務器端的所有參數。 –
現在我正在調用未定義的方法Illuminate \ Support \ Facades \ Request :: input()' – Halnex