model->orderBy('status_id', 'desc') ->orderBy('due_date') ->where(function ($query) use ($q, $customerId) { if ($customerId) { $query->where('customer_id', $customerId); } if ($q) { $query->where('name', 'like', '%'.$q.'%'); } }) ->with('customer', 'vendor') ->paginate($this->_paginate); } }