diff --git a/app/Http/Controllers/Admin/PirepController.php b/app/Http/Controllers/Admin/PirepController.php index 5b80af67..21a7a79c 100644 --- a/app/Http/Controllers/Admin/PirepController.php +++ b/app/Http/Controllers/Admin/PirepController.php @@ -190,9 +190,7 @@ class PirepController extends Controller $this->pirepRepo->pushCriteria($criterea); $pireps = $this->pirepRepo - ->findWhere(['status', '!=', PirepState::CANCELLED]) - ->findWhere(['status', '!=', PirepState::DRAFT]) - ->orderBy('created_at', 'desc') + ->whereNotInOrder('status', [PirepState::CANCELLED, PirepState::DRAFT], 'created_at', 'desc') ->paginate(); return view('admin.pireps.index', [ diff --git a/app/Interfaces/Repository.php b/app/Interfaces/Repository.php index d29b7d6b..e9dba006 100644 --- a/app/Interfaces/Repository.php +++ b/app/Interfaces/Repository.php @@ -76,4 +76,29 @@ abstract class Repository extends \Prettus\Repository\Eloquent\BaseRepository return $q; }); } + + /** + * Find records with a WHERE clause but also sort them + * @param string $col + * @param array $values + * @param string $sort_by + * @param string $order_by + * @return $this + */ + public function whereNotInOrder($col, $values, $sort_by, $order_by = 'asc') + { + return $this->scopeQuery(function ($query) use ($col, $values, $sort_by, $order_by) { + $q = $query->whereNotIn($col, $values); + # See if there are multi-column sorts + if (\is_array($sort_by)) { + foreach ($sort_by as $key => $sort) { + $q = $q->orderBy($key, $sort); + } + } else { + $q = $q->orderBy($sort_by, $order_by); + } + + return $q; + }); + } }