add whereNotInOrder() to repository

This commit is contained in:
Nabeel Shahzad
2018-04-16 16:33:41 -05:00
parent 63c5d69e10
commit 0965465601
2 changed files with 26 additions and 3 deletions

View File

@@ -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', [

View File

@@ -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;
});
}
}