2015-10-16 27 views
0

我在我的帖子頁面上有一個文本區域輸入字段,我試圖用它來通過ajax提交用戶註釋。但我不斷收到SQL錯誤註釋字段不能爲空。我確信我正在填充數據。Laravel 5:無法從視圖中檢索用戶輸入

SQLSTATE [23000]:完整性約束違規:1048列 '評論' 不能爲空(SQL:插入commentsuser_idcommentparent_idparentsupdated_atcreated_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 
}); 

回答

1

嘗試

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

更改爲這種格式的工作。

+0

沒有工作。我得到'未定義的屬性:Illuminate \ Support \ Facades \ Request :: $ commenter_comment' – Halnex

+0

對不起,我的壞。我編輯了我的答案。嘗試這個。如果這不起作用。在你的post_this_comment裏面寫dd($ request); 。通過這個,你將能夠看到所有的接收服務器端的所有參數。 –

+0

現在我正在調用未定義的方法Illuminate \ Support \ Facades \ Request :: input()' – Halnex

1

如果使用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條件檢查輸入是否實際發送並且有一個值。在嘗試使用它之前,這很有用。

+0

我已經嘗試過了,如果給出null,那麼在dd($ request-> input('commenter_comment'))' – Halnex

+0

仍然給出null,那麼請求將該值作爲null發送。你能否再次檢查請求或在這裏發佈請求數據? – codegeek