1 回答
TA贡献1780条经验 获得超3个赞
您可以local在模型上定义范围来重构查询。以下是一些:
class Ticket extends Model
{
use SoftDeletes;
public function scopeByAuthUser($query)
{
return $query->where('user_id','=', \Auth::user()->id);
}
public function scopeOpen($query)
{
return $query->where('status_id', 1);
}
public function scopePending($query)
{
return $query->where('status_id', 2);
}
public function scopeClose($query)
{
return $query->where('status_id', 2);
}
}
以下是重构您的条件的方法:
// for the first query
$tickets = Ticket::with('users','ticketStatus','ticketType','tbl_contacts')
->byAuthUser()
->latest();
if(request('Open') || request('Pending') || request('Close')) {
$scope = strtolower(request('Open') ?? request('Pending') ?? request('Close'));
$tickets = $tickets->{$scope}()->get();
} else {
$tickets = $tickets->get();
}
- 1 回答
- 0 关注
- 76 浏览
添加回答
举报