如何避免php代码重复在这里


How to avoid php code duplication here

我正在创建一个laravel todo应用程序。在我的控制器中有不同的方法,但其中的所有代码几乎相同。在notCompleted方法和completed方法中,还有1个不同的where子句。除此之外,所有代码都是相同的。如何避免这里的代码重复?

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

我需要三个不同的方法,所以我保留了这些方法,并将代码提取到一个方法中。也许这种保存是一种代码复制。不是吗?

感谢所有回复我的人:)

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

像这样的东西?并且只通过函数调用上的数据

您可以创建一个简单的函数,接受几个可变变量

所以类似于:

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

在您的模型中,您可以定义一个范围来消除重复。示例

在Todo模型

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;
 }

创建一个函数以获取ByCompletedStatus。。。

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

然后,您将根据需要传递true或false,以避免代码重复。