Support the ongoing development of Laravel.io →
posted 7 years ago

Hello guys !

I'm trying to learn Laravel framework, so i'm doing a tutorial and i have a problem with my "comment section",

Models: Comment.php (Here is the error line 8)


  	public static function boot(){
    		parent::boot();
    		self::deleted(function($comment){
    			$comment->post->counts_comment = $comment->post->comments->count();
    			$comment->post->save();
    		});
    		self::created(function($comment){
    		        $comment->post->counts_comment = $comment->post->comments->count();
    			$comment->post->save();
    		});

		return true;
	}

Controller: CommentsController.php

		public function create($id){
			  $post = Post::find($id);
				$inputs = Input::all();
				Comment::create([
						'user_id' => Auth::user()->id,
						'post_id' => $post->id,
						'content' => $inputs['comment'],
				]);
				return Redirect::back()->with('success','Votre commentaire a bien été créé');
			}

Route: routes.php

Route::group(['before'=>'auth'],function(){
  Route::post('posts/{id}/comments/create',['as'=>'comments.create','uses'=>'CommentsController@create']);
});

Thanks for you attention !

Last updated 3 years ago.
0

I would guess either $comment->post or $comment->post->comments is undefined (null). Try adding the line dd($comment->post) which will stop execution and dump out the details of that variable to check if it is defined. If so, change it to dd($comment->post->comments) and make sure that one is too.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.