Backend List Filter To Toggle Global Scope
I have a model for job vacancies which has a global scope to only show open vacancies, but in the backend I want a checkbox to toggle this so that all vacancies can be shown. The checkbox type only seems to work with local scopes. Can anyone help?
Answer
You could setup a local scope on your model that removes the global scope.
Something like:
public function scopeAllVacancies($query)
{
// This is not actually how you remove a global scope,
// see https://softonsofa.com/laravel-5-eloquent-global-scope-how-to/
// for more information.
$query->removeScope(MyGlobalScope);
}
As a side note, you won't be able to use the remove method suggested in https://softonsofa.com/laravel-5-eloquent-global-scope-how-to/ as the list widget is using the object through reference, and won't listen to the return value of this scope. So, in order to remove it currently (at least until we get some form of $query->resetQuery()
or $query->removeScope($scope)
implemented), you'll have to perform the removal of the scope manually on the query object.
Here's an example of interfacing directly with the query object in order to manually remove a scope:
/**
* Removes the provided orderby column rule from the QueryBuilder orders array to reset the orderBy for that column
*
* @param Builder $query
* @param string $orderByColumn
* @return Builder $query
*/
public function scopeRemoveOrder($query, $orderByColumn)
{
// Orders property can have mixed up integer index, flatten the array to negate any weird keys in this array
$orders = $query->getQuery()->orders;
if (is_array($orders)) {
$orders = array_values($query->getQuery()->orders);
$i = 0;
foreach ($orders as $order) {
if ($order['column'] === $orderByColumn) {
unset($orders[$i]);
}
$i++;
}
$query->getQuery()->orders = $orders;
}
return $query;
}
Related Questions
- → OctoberCMS Backend Loging Hash Error
- → "failed to open stream" error when executing "migrate:make"
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → Create plugin that makes objects from model in back-end
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → How to install console for plugin development in October CMS
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → How to update data attribute on Ajax complete
- → October CMS - Conditionally Load a Different Page