私はlaravel todoアプリケーションを作成しています。私のコントローラーにはさまざまな方法がありますが、その中のすべてのコードはほとんど同じです。 notCompletedメソッドと完了メソッドには、1つの異なるwhere句があります。それ以外はすべて同じです。ここでコードの重複を避けるにはどうすればよいですか?ここでPHPコードの重複を避けるには
public function all()
{
$user_id = $this->user_id;
$todos = $this->todos
->where('user_id', $user_id)
->orderBy('id', 'DESC')->paginate(15);
return view('todos.index', compact('todos'));
}
public function notCompleted()
{
$user_id = $this->user_id;
$todos = $this->todos
->where('user_id', $user_id)
->where('completed', false)
->orderBy('id', 'DESC')->paginate(15);
return view('todos.index', compact('todos'));
}
public function completed()
{
$user_id = $this->user_id;
$todos = $this->todos
->where('user_id', $user_id)
->where('completed', true)
->orderBy('id', 'DESC')->paginate(15);
return view('todos.index', compact('todos'));
}
私はこれを行っている可能性が、私は3つのルートのための3つの別々の方法を必要としています。応答をありがとう –