我正在创建一个laravel todo应用程序。在我的控制器中,有不同的方法,但其中的所有代码几乎相同。在notCompleted方法和已完成的方法中,还有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'));
}
答案 0 :(得分: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'));
}
}
答案 1 :(得分:0)
在您的模型中,您可以定义范围以消除重复。示例
在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;
}
答案 2 :(得分: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'));
}
}
这样的事情?并只传递函数调用上的数据
答案 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'));
}
答案 4 :(得分:0)
为getByCompletedStatus创建一个函数......
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,并避免代码重复。