简体   繁体   中英

Laravel5: retrieving post_id from a hidden input returns null

I have a working comment form with $post->id that submits data via ajax, notice there isn't any form tags.

<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>

And this is the javascript handler

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

All I want to do is get the post id of the current post so I can tell the comment_list() to get me only the comments of that post.

I cannot even get the value of commenter_post from comment_list() method, I get null. But I am able to retrieve the values from other methods just fine.

so I have added a hidden field (without form tags) to a partial that retrieves the post_id

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

However, when I try to get the value of that field, I always get null

Comment Model

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

Index() method on CommentController

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

Try dumping the whole request object and look over the parameters in the POST or GET request, make sure your field is there, if not something might be wrong with the form.

Is that hidden field, inside an actual form that is submitted before the comments_list function is called?

You've tagged Laravel-5 for the question, but are using the Laravel 4 method of retrieving input . For Laravel 5 input retrieval , try:

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

Additionally, per the Controller help page , your route arguments should come after your other dependancies.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM