LaravelPackages.net
Acme Inc.
Toggle sidebar
almirhodzic/nova-toggle-5

A Laravel Nova 5 toggle field that allows quick boolean updates directly from the index view.

2.402
3
v1.3.1
About almirhodzic/nova-toggle-5

almirhodzic/nova-toggle-5 is a Laravel package for a laravel nova 5 toggle field that allows quick boolean updates directly from the index view.. It currently has 3 GitHub stars and 2.402 downloads on Packagist (latest version v1.3.1). Install it with composer require almirhodzic/nova-toggle-5. Discover more Laravel packages by almirhodzic or browse all Laravel packages to compare alternatives.

Last updated

Laravel Nova 5 Toggle Field

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.

Laravel Nova 5 Toggle Field

License: MIT Nova 5 PHP 8.2+ Downloads

Features

  • 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

Requirements

  • PHP 8.2+
  • Laravel Nova 5.x

Installation

composer require almirhodzic/nova-toggle-5

The service provider will be automatically registered.

Upgrading from 1.2 to 1.3

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:api middleware. Only users who pass the viewNova gate can reach it.
  • The controller additionally checks the resource's authorizedToUpdate policy and only allows writes to attributes that are actually exposed as a Toggle field on the resource.
  • The config/nova-toggle-5.php file (with its guards option) 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.

Basic Usage

use AlmirHodzic\NovaToggle5\Toggle;

public function fields(NovaRequest $request)
{
    return [
        Toggle::make('Active', 'is_active'),
    ];
}

Configuration

Colors

Toggle Background Colors

Toggle::make('Active', 'is_active')
    ->onColor('#00d5be', '#009689')  // Light mode, Dark mode
    ->offColor('#e5e5e5', '#323f57');

Bullet Colors

Toggle::make('Active', 'is_active')
    ->onBullet('white')           // Same for both modes
    ->offBullet('white', 'grey'); // Light mode, Dark mode

Labels

Custom ON/OFF Text

Toggle::make('Active', 'is_active')
    ->valueLabelText('ON', 'OFF');  // ON label, OFF label

Label Colors

Toggle::make('Active', 'is_active')
    ->valueLabelText('ON', 'OFF')
    ->valueLabelOnColors('#ffffff')              // ON label color
    ->valueLabelOffColors('#a1a1a1', '#737373'); // OFF label colors (light, dark)

Toast Notifications

Custom Toast Label

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 default

Default fallback order: namelabeltitle → resource singular label

Disable Toast Notifications

Toggle::make('Active', 'is_active')
    ->toastShow(false); // No toast notification on toggle

Filtering

To make your toggle field filterable in the index view, you need to create a custom filter.

Step 1: Create a Filter

php artisan nova:filter IsActiveFilter

Step 2: Implement the Filter

<?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',
        ];
    }
}

Step 3: Register the Filter in Your Resource

use App\Nova\Filters\IsActiveFilter;

public function filters(NovaRequest $request): array
{
    return [
        new IsActiveFilter,
    ];
}

Alternative: Boolean Filter (Checkboxes)

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()
    ...

Help Text

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');

Visibility & Access Control

Hide Based on Condition

Toggle::make('Active', 'is_active')
    ->hideWhen(function ($request, $resource) {
        return $resource->status === 'archived';
    });

Readonly Based on Condition

Toggle::make('Active', 'is_active')
    ->readonlyWhen(function ($request, $resource) {
        return !$request->user()->isAdmin();
    });

Authorization

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.

Complete Example

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,
    ];
}

API Reference

Methods

| 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 |

Default Colors

| 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 |

Default Behavior

| Option | Default Value | | --------------- | ---------------------------------------------------------------------- | | toastShow | true | | toastLabelKey | null (uses fallback: name → label → title → resource singular label) |

Bug or Issues

Found a Bug or Issue? Please report here: GitHub Issues
We appreciate your feedback to help improve this package.

Support

License

The MIT License (MIT). Please see License File for more information.

Credits


By Frontbyte

Comments