This Laravel Nova 5 Toggle Field removes the detour into the edit page and lets you flip booleans directly in the index. Fewer clicks, less context switching, smoother workflow.
- Quick toggle directly from index view
- Customizable colors for light and dark mode
- Built-in readonly and visibility controls
- Optional help text for different views
- Custom ON/OFF labels with color customization
- Optional toast notification control
- Customizable toast message labels
- Filter support for index views
- Logs the update action in Nova's action events
- Vue 3 Composition API
- Full dark mode support
- PHP 8.2+
- Laravel Nova 5.x
composer require almirhodzic/nova-toggle-5The service provider will be automatically registered.
Version 1.3.0 contains a security fix that changes how the toggle endpoint is
authorized. For most installations the upgrade is a drop-in: just run
composer update almirhodzic/nova-toggle-5.
What changed:
- The toggle endpoint is now protected by Nova's own
nova:apimiddleware. Only users who pass theviewNovagate can reach it. - The controller additionally checks the resource's
authorizedToUpdatepolicy and only allows writes to attributes that are actually exposed as aTogglefield on the resource. - The
config/nova-toggle-5.phpfile (with itsguardsoption) has been removed. If you previously published it, you can delete it — it is no longer read.
If you were relying on the old guards config to grant toggle access to users
that do not pass the viewNova gate, make sure those users are now either
allowed by the gate or no longer need toggle access.
use AlmirHodzic\NovaToggle5\Toggle;
public function fields(NovaRequest $request)
{
return [
Toggle::make('Active', 'is_active'),
];
}Toggle::make('Active', 'is_active')
->onColor('#00d5be', '#009689') // Light mode, Dark mode
->offColor('#e5e5e5', '#323f57');Toggle::make('Active', 'is_active')
->onBullet('white') // Same for both modes
->offBullet('white', 'grey'); // Light mode, Dark modeToggle::make('Active', 'is_active')
->valueLabelText('ON', 'OFF'); // ON label, OFF labelToggle::make('Active', 'is_active')
->valueLabelText('ON', 'OFF')
->valueLabelOnColors('#ffffff') // ON label color
->valueLabelOffColors('#a1a1a1', '#737373'); // OFF label colors (light, dark)By default, the toast message uses the resource's name, label, title, or the resource's singular label. You can customize which model attribute to use:
Toggle::make('Show', 'show')
->toastLabelKey('question'); // Uses $model->question instead of defaultDefault fallback order: name → label → title → resource singular label
Toggle::make('Active', 'is_active')
->toastShow(false); // No toast notification on toggleTo make your toggle field filterable in the index view, you need to create a custom filter.
php artisan nova:filter IsActiveFilter<?php
namespace App\Nova\Filters;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Laravel\Nova\Filters\Filter;
use Laravel\Nova\Http\Requests\NovaRequest;
class IsActiveFilter extends Filter
{
public $name = 'Active Status';
public $component = 'select-filter';
public function apply(NovaRequest $request, Builder $query, mixed $value): Builder
{
if ($value === 'active') {
return $query->where('is_active', true);
}
if ($value === 'inactive') {
return $query->where('is_active', false);
}
return $query;
}
public function options(NovaRequest $request): array
{
return [
'Active' => 'active',
'Inactive' => 'inactive',
];
}
}use App\Nova\Filters\IsActiveFilter;
public function filters(NovaRequest $request): array
{
return [
new IsActiveFilter,
];
}If you prefer checkboxes instead of a dropdown:
<?php
namespace App\Nova\Filters;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Laravel\Nova\Filters\BooleanFilter;
use Laravel\Nova\Http\Requests\NovaRequest;
class IsActiveFilter extends BooleanFilter
{
public $name = 'Active Status';
public function apply(NovaRequest $request, Builder $query, mixed $value): Builder
{
if (isset($value['active'])) {
return $query->where('is_active', $value['active']);
}
if (isset($value['inactive'])) {
return $query->where('is_active', !$value['inactive']);
}
return $query;
}
public function options(NovaRequest $request): array
{
return [
'Active' => 'active',
'Inactive' => 'inactive',
];
}
}Now use
Toggle::make('Active', 'is_active')
->filterable()
...Add contextual help text for different views:
Toggle::make('Active', 'is_active')
->helpOnIndex('Toggle to activate/deactivate')
->helpOnForm('Enable this option to activate the feature')
->helpOnDetail('Current activation status');Toggle::make('Active', 'is_active')
->hideWhen(function ($request, $resource) {
return $resource->status === 'archived';
});Toggle::make('Active', 'is_active')
->readonlyWhen(function ($request, $resource) {
return !$request->user()->isAdmin();
});Toggle requests are protected by Nova's own API middleware, so only users who pass Nova's
viewNova gate can reach the endpoint. On top of that, the controller enforces the
resource's authorizedToUpdate policy and only allows writes to attributes that are actually
exposed as a Toggle field on the resource (and are not readonly in the current context).
No additional configuration is required — the toggle follows the same access rules as Nova itself.
use AlmirHodzic\NovaToggle5\Toggle;
use App\Nova\Filters\IsActiveFilter;
public function fields(NovaRequest $request)
{
return [
ID::make()->sortable(),
Text::make('Name'),
Toggle::make('Active', 'is_active')
->filterable()
->onColor('#10b981', '#059669')
->offColor('#ef4444', '#dc2626')
->onBullet('#ffffff')
->offBullet('#ffffff')
->valueLabelText('ON', 'OFF')
->valueLabelOnColors('#ffffff')
->valueLabelOffColors('#fecaca', '#fca5a5')
->helpOnIndex('Click to toggle status')
->helpOnForm('Enable to make this item visible')
->toastShow(true)
->readonlyWhen(function ($request, $resource) {
return !$request->user()->can('edit', $resource);
}),
Toggle::make('Featured', 'is_featured')
->onColor('#f59e0b')
->offColor('#6b7280')
->valueLabelText('★', '☆')
->toastShow(false)
->hideWhen(function ($request, $resource) {
return !$resource->is_active;
}),
Toggle::make('Show FAQ', 'show')
->toastLabelKey('question') // Uses $faq->question for toast message
->helpOnIndex('Toggle visibility'),
];
}
public function filters(NovaRequest $request): array
{
return [
new IsActiveFilter,
];
}| Method | Parameters | Description |
|---|---|---|
onColor() |
string $light, ?string $dark = null |
Background color when ON |
offColor() |
string $light, ?string $dark = null |
Background color when OFF |
onBullet() |
string $light, ?string $dark = null |
Bullet color when ON |
offBullet() |
string $light, ?string $dark = null |
Bullet color when OFF |
valueLabelText() |
?string $onLabel = 'ON', ?string $offLabel = 'OFF' |
Custom label text |
valueLabelOnColors() |
string $light, ?string $dark = null |
ON label color |
valueLabelOffColors() |
string $light, ?string $dark = null |
OFF label color |
toastShow() |
bool $show = true |
Show/hide toast notification on toggle |
toastLabelKey() |
string $key |
Model attribute to use for toast label |
hideWhen() |
callable $callback |
Hide field based on condition |
readonlyWhen() |
callable $callback |
Make readonly based on condition |
helpOnIndex() |
string $text |
Help text on index view |
helpOnForm() |
string $text |
Help text on form view |
helpOnDetail() |
string $text |
Help text on detail view |
| State | Light Mode | Dark Mode |
|---|---|---|
| ON Background | #00d5be |
#009689 |
| OFF Background | #e5e5e5 |
#323f57 |
| ON Bullet | #ffffff |
#ffffff |
| OFF Bullet | #ffffff |
#ffffff |
| ON Label | #ffffff |
#ffffff |
| OFF Label | #a1a1a1 |
#737373 |
| Option | Default Value |
|---|---|
toastShow |
true |
toastLabelKey |
null (uses fallback: name → label → title → resource singular label) |
Found a Bug or Issue? Please report here: GitHub Issues
We appreciate your feedback to help improve this package.
The MIT License (MIT). Please see License File for more information.
By Frontbyte
