samedi 17 octobre 2015

Laravel 5: Trying to get property of non-object through method injection

I am using Route-Model-Binding with method injection to retrieve the $post->id of the post I am commenting on, but I keep getting this error

Trying to get property of non-object (View: C:\xampp\htdocs\reddit\resources\views\partials\post.blade.php) (View: C:\xampp\htdocs\reddit\resources\views\partials\post.blade.php)

This is the method I have, injecting Post $post then trying to get $post->id

dd($post) retrieves nothing at all even if I dump the model alone.

public function post_this_comment(Request $request, Post $post) {
    $post = Post::whereId($post->id)->first(); // this is where I'm getting the post

    $comment = new Comment;
    $comment->user_id = Auth::id();
    $comment->comment = $request->input('commenter_comment');
    $comment->parent_id = $request->input('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 = $request->input('per_page');

    $comment_list = view('post/show')
        ->with('comments', $this->comment_list($per_page, $request))
        ->with('total_comments', $this->total_comments())
        ->with('per_page', $per_page)
        ->with('post', $post)
        ->render();

    $response = array(
        'status' => 'success',
        'msg' => 'Comment Saved!',
        'comment_list' => $comment_list
    );

    return Response::json($response);
}

This is the view, I have opened a form even though I don't need it because I'm submitting data via ajax.

{!! Form::open(['route' => ['post_this_comment', $post]]) !!}
            <div class="comment-fields">
                <div class="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="commenter-name-email">
                    <input type="hidden" id="commenter_parent" name="commenter_parent" class="commenter-parent" value="0">
                </div>

                <div class="commenter-captcha">
                    <div class="col-md-3 text-right">
                        <a href="javascript:void(0)" class="btn btn-info post-this-comment">Comment</a>
                    </div>
                </div>
  {!! Form::close() !!}

Routes

Route::resource('posts', 'PostsController');
Route::post('{post}/post_this_comment', ['as' => 'post_this_comment', 'uses' => 'PostsController@post_this_comment']);

RouteServiceProvider

$router->model('articles', 'App\Article');
$router->model('subreddit', 'App\Subreddit');
$router->model('posts', 'App\Post');
$router->model('moderators', 'App\Moderator');
$router->model('comments', 'App\Comment');



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Kgsl8s
via IFTTT

Aucun commentaire:

Enregistrer un commentaire