08-06-2023, 04:28 PM
I'm using Lumen as REST API. I have a Post model and a Comment Model.
Post.php
public function comments(){
return $this->hasMany('App\Comment');
}
Comment.php
public function post(){
return $this->belongsTo('App\Post');
}
I also have Resource controllers for both these models. If I want to get all the comments on a particular post, where do I write that logic? In the PostController (because I'll be filtering comments by Post) or in the CommentController (because I'm fetching comments after all)? Which is the better way? Or is there some other way altogether (like creating a separate controller) ? I just want to get the structure right and write clean code. Any help will be greatly appreciated.
Post.php
public function comments(){
return $this->hasMany('App\Comment');
}
Comment.php
public function post(){
return $this->belongsTo('App\Post');
}
I also have Resource controllers for both these models. If I want to get all the comments on a particular post, where do I write that logic? In the PostController (because I'll be filtering comments by Post) or in the CommentController (because I'm fetching comments after all)? Which is the better way? Or is there some other way altogether (like creating a separate controller) ? I just want to get the structure right and write clean code. Any help will be greatly appreciated.