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 !
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community