2016-04-03 22 views
0

私は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'));   
} 

答えて

1

私は3つの異なる方法が必要でしたので、私はそれらの方法を保ち、コードを1つの方法に抽出しました。そして、その保存はコードの重複かもしれません。ではない?

と私に応じるすべての人に感謝します:)

public function all() 
{ 
    return $this->todoStatus('all'); 
} 

public function index() 
{ 
    return $this->todoStatus('current', false); 
} 

public function completed() 
{ 
    return $this->todoStatus('completed', true); 
} 

protected function todoStatus($completed, $status = false) 
{ 
    $user_id = $this->user_id; 

    if($completed === 'all') { 
     $todos = $this->todos 
      ->where('user_id', $user_id) 
      ->orderBy('id', 'DESC')->paginate(15); 
     return view('todos.index', compact('todos')); 
    } else { 
     $todos = $this->todos 
      ->where('user_id', $user_id) 
      ->where('completed', $status) 
      ->orderBy('id', 'DESC')->paginate(15); 
     return view('todos.index', compact('todos'));  
    } 
} 
0

モデルでは、複製を削除するためのスコープを定義できます。例

藤堂モデルで

public function scopeUserTodo($query, $userId){ 
    return $query->where('user_id', $userId); 
} 

public function scopeCompleted($query, $flag){ 
    return $query->where('completed', $flag); 
} 

public function scopeLatest($query){ 
    return $query->orderBy('id','Desc'); 
} 

は、その後、あなたのコントローラであなたは

public function all() 
{ 
    $user_id = $this->user_id; 

    $todos = $this->todos->userTodo($user_id)->latest()->paginate(15) 

    return view('todos.index', compact('todos')); 
} 


public function notCompleted() 
{ 
    $user_id = $this->user_id; 

    $todos = $this->todos->userTodo($user_id)->completed(false) 
      ->latest()->paginate(15) 


    return view('todos.index', compact('todos')); 
} 


public function completed() 
{ 
    $user_id = $this->user_id; 

     $todos = $this->todos->userTodo($user_id)->completed(true) 
      ->latest()->paginate(15) 

    return view('todos.index', compact('todos'));   
} 

にあなたのクエリを変更することができますあなたは、さらにあなたのコントローラ内の関数を作成し、todoStateという名前を付け、共通のコードを移動することができますそこではnotCompleted関数とcompleted関数の間にあります。例

public function todoState($userId, $completed){ 

    $todos = $this->todos->userTodo($userId) 
       ->completed($completed) 
       ->latest() 
       ->paginate(15); 

    return $todos; 

} 
0
public function all($check, $value) 
{ 
    $user_id = $this->user_id; 

    if($check !== "completed"){ 
    $todos = $this->todos 
      ->where('user_id', $user_id) 
      ->where('completed', $value) 
      ->orderBy('id', 'DESC')->paginate(15); 

    return view('todos.index', compact('todos')); 
    }else{ 
     $todos = $this->todos 
      ->where('user_id', $user_id) 
      ->orderBy('id', 'DESC')->paginate(15); 

      return view('todos.index', compact('todos')); 
    } 
} 

このような何か?そしてちょうど

+0

私はこれを行っている可能性が、私は3つのルートのための3つの別々の方法を必要としています。応答をありがとう –

0

あなたは、いくつかの変更変数にとる単純な関数を作成することができ、関数呼び出しのデータを通過

そうのようなもの:お使いのコントローラのコードで次に

private function helper($userid, $completed) 
{ 
    return $this->todos 
     ->where('user_id', $userid) 
     ->where('completed', $completed) 
     ->orderBy('id', 'DESC')->paginate(15); 
} 

public function notCompleted() 
{ 
    $user_id = $this->user_id; 

    $todos = $this->helper($user_id, false); 

    return view('todos.index', compact('todos')); 
} 
+0

私はこれを行うことができますが、私は3つのルートのための3つの別々の方法が必要です。応答をありがとう –

+0

私はあなたが各ルートのコード量を減らすことを考えていると思っていました。私はあなたがコード複製の量を減らすために、あなたの3つのルートのそれぞれからヘルパーを呼び出すことができたことを意味していたすべての3をただ1つ置き換えることはありません:)クリアされていないために申し訳ありません:) – Graeme

0

getByCompletedStatus ...

に関数を作成します。

次に、必要に応じてtrueまたはfalseを渡し、コードの重複を避けます。

+0

私は、 3つのルートのメソッド。 ありがとうございます –

関連する問題