【问题标题】:Laravel 5: cannot retrieve user input from viewLaravel 5:无法从视图中检索用户输入
【发布时间】:2015-10-16 19:46:51
【问题描述】:

我的帖子页面上有一个文本区域输入字段,我试图用它来通过 ajax 提交用户 cmets。但我不断收到 SQL 错误注释字段不能为空。我很肯定我正在用数据填充该字段。

SQLSTATE[23000]:完整性约束违规:1048 列“注释”不能为空(SQL:插入commentsuser_idcommentparent_idparentsupdated_at,@ 987654328@) 个值 (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_comment500(内部服务器错误)

这些是我的路线

Route::resource('posts', 'PostsController');
Route::post('posts/post_this_comment', 'PostsController@post_this_comment');

PostsController中的post_this_comment()方法

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

【问题讨论】:

    标签: php laravel routes laravel-5


    【解决方案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 
    });
    

    更改为这种格式有效。

    【讨论】:

    • 没用。我得到Undefined property: Illuminate\Support\Facades\Request::$commenter_comment
    • 对不起,我的错。我已经编辑了我的答案。试试这个。如果这不起作用。在你的 post_this_comment 里面只写 dd($request); .通过这个你可以看到你是否在服务器端接收所有参数。
    • 现在我收到了Call to undefined method Illuminate\Support\Facades\Request::input()
    • 确保你有类型提示使用 Illuminate\Http\Request;只需在顶部写下这一行。并删除使用 Illuminate\Support\Facades\Request;行。
    • 我在提交时停止收到错误,但 dd($comment-&gt;comment); 仍然给出 null
    【解决方案2】:

    如果您使用的是 Laravel 5.x,请不要使用 Input::get(),因为版本 5 及更高版本建议使用 $request-&gt;input('something')

    您必须使用以下内容:

    use Illuminate\Http\Request;
    

    那么你可以使用:

    if($request->has('commenter_comment'))
    {
        $comment->comment = $request->input('commenter_comment');
    }
    

    上面的 if 条件检查输入是否实际发送并具有值。在您尝试使用它之前,这很有用。

    【讨论】:

    • 我已经尝试过了,仍然在dd($request-&gt;input('commenter_comment'))上给出null
    • 如果这给您 null,那么请求将该值作为 null 发送。您可以再次检查请求或在此处发布请求数据吗?
    猜你喜欢
    • 2018-06-11
    • 1970-01-01
    • 2021-02-11
    • 2018-04-01
    • 2015-09-02
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 2018-12-06
    相关资源
    最近更新 更多