Laravel global scopes and Nova

I have recently been building a new application using Laravel and Nova.

For the front-end of the application, I would only like to restrict models to those that are "active". To achieve this, I used the global scope behaviour which makes sure every query applies certain constraints.

The documentation has examples for both global and anonymous global scopes. For brevity, below is an example anonymous global scope for an "active" state for a Page model.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class Page extends Model
{
    protected static function booted(): void
    {
        static::addGlobalScope('active', static function (Builder $builder): void {
            $builder->where('active', '=', 1);
        });
    }
}

This is perfect for the front-end, as I am confident that when I get all Page models or a specific one, I know I will only be getting those which are active.

However, this has a side effect in the management of these models. In Nova – the tool I am using for a content management system – I now only see models which are active. This isn't useful as I might have draft pages that I can no longer manage.

When installing Nova, it should have generated an abstract Resource class which your resources extend. In this class, there are different query methods which are calling their parent.

To derestrict any global scopes you might have applied, you can update the indexQuery method with the following;

public static function indexQuery(NovaRequest $request, $query)
{
    $query->withoutGlobalScopes();

    return parent::indexQuery($request, $query);
}

Now all your model data will show up in Nova.