CommentsController
内のstore
とdestroy
において
$post = Post::findOrFail($postId);
とあります。
下記の様に$post
を作らない実装も可能かと思いますが、$post
を生成して実装した方が良い理由を教えていただきたいです。
// storeメソッド
public function store(Request $request, $postId){
$this->validate($request, [
'body' => 'required'
]);
$comment = new Comment(['post_id'=>$postId, 'body'=>$request->body]);
$comment->save();
return redirect()->action('PostsController@show', ['post'=>$postId]);
}
// destroyメソッド
public function destroy($postId, $commentId){
$comment = Comment::where('id', $commentId);
//もしくは
//$comment = Comment::where('post_id', $postId)->where('id', $commentId);
$comment->delete();
return redirect()->action('PostsController@show', ['post'=>$postId]);
}
私の考えでは、comment
はpost
に紐付いているので、comment
だけで(小手先で)処理するやり方よりも、post
を中心においた処理の仕方の方がこの実装以降の拡張性が高くなったり、応用が効きやすくなるためかなと考えました。例えば user
やlike
機能を実装するとなった時のため等が考えられます...。
この理解があっているか教えていただけますでしょうか。
この回答を見るにはプレミアムプランへの登録が必要です
プレミアムプランとは?