Laravel5:从隐藏的输入中检索post_id返回null


Laravel5: retrieving post_id from a hidden input returns null

我有一个通过ajax提交数据的$post->id的工作评论表单,注意没有任何表单标签。

<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">
        <input type="hidden" id="commenter_post" name="commenter_post" class="commenter-post" value="{{ $post->id }}">
    </div>
    <div class="row commenter-captcha">
        <div class="col-md-3">
            <a href="javascript:void(0)" class="btn btn-success post-this-comment">Comment</a>
        </div>
    </div>
</div>

这是javascript处理程序

$(document).on('click', 'a.post-this-comment', function(){
    var form_data = {
        'per_page': $('.comments_per_page').val(),
        'commenter_parent': $('#commenter_parent').val(),
        'commenter_post': $('#commenter_post').val(),
        'commenter_comment': $('#commenter_comment').val(),
    };
    var arr = [
        'commenter_parent',
        'commenter_post',
        'commenter_comment'
    ];
    for (var i in arr, i < arr.length, i++) {
        var elem = arr[i];
        form_data[elem] = $('#' + elem).val();
    }
// console.log(form_data); // something like => Object {per_page: "some_value", commenter_parent: "some_value", commenter_user_id: "some_value", commenter_comment: "some_value"}
    var request = $.ajax({
        type: 'POST',
        url: 'post_this_comment',
        data: form_data,
        dataType: 'json'
    });
    request.done(comment_done_handler);
    request.fail(comment_fail_handler);
});

我想要做的就是获取当前帖子的帖子id,这样我就可以告诉comment_list()只获取该帖子的评论。

我甚至不能从comment_list()方法中得到commenter_post的值,我得到null。但是我可以从其他方法中检索值。

所以我添加了一个隐藏字段(没有表单标签),以部分检索post_id

<input type="hidden" id="post_id" name="post_id" class="post-id" value="{{ $post->id }}">

然而,当我试图获得该字段的值时,我总是得到null

Comment模型

public static function root_comments($postId) { // setting $postId
    return self::child_comments(0, 'desc')->where('post_id', $postId);
}

CommentController

protected function comment_list($per_page, Request $request) {
    $post = Input::get('post_id');
    dd($post); // returns null
    $root_comments = Comment::root_comments(1); // I am setting the postId manually here
    $root_with_replies = $this->include_replies_for($root_comments);
    $paginated_comments = $this->paginate($root_with_replies, $per_page, $request);
    return $paginated_comments;
}

CommentController上的Index()方法

public function index(Request $request) {
        $view_data = self::view_data($request);
        return view('eastgate.comment.leave_a_comment', $view_data);
}

尝试转储整个请求对象,并查看POST或GET请求中的参数,确保您的字段在那里,否则表单可能有问题。

是隐藏的字段,在一个实际的表单中,在comments_list函数被调用之前提交?

您已经为问题标记了Laravel-5,但正在使用Laravel 4方法检索输入。对于Laravel 5的输入检索,尝试:

$post = $request->input('post_id');

此外,根据Controller帮助页面,你的路由参数应该在你的其他依赖项之后。

CommentController

protected function comment_list(Request $request, $per_page) {
    $post = $request->input('post_id');
    dd($post);
    $root_comments = Comment::root_comments(1);
    $root_with_replies = $this->include_replies_for($root_comments);
    $paginated_comments = $this->paginate($root_with_replies, $per_page, $request);
    return $paginated_comments;
}