1 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以local在模型上定義范圍來重構(gòu)查詢。以下是一些:
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);
}
}
以下是重構(gòu)您的條件的方法:
// 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 關(guān)注
- 100 瀏覽
添加回答
舉報(bào)