Backend list filter to toggle global scope

- 1 answer


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?




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
    // for more information.

As a side note, you won't be able to use the remove method suggested in 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) {
        $query->getQuery()->orders = $orders;
    return $query;