Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Toggle Field For Backpack Laravel Package

digitallyhappy/toggle-field-for-backpack

A toggle switch field for Backpack for Laravel admin panels, letting you edit boolean columns with a clean on/off switch instead of a checkbox. Drop it into your field definitions to make true/false settings faster and clearer to manage.

View on GitHub
Deep Wiki
Context7

Getting Started

  • Install with composer require digitallyhappy/toggle-field-for-backpack. Use 2.0.1 or later if you're on Backpack v5 — v5 support landed in this release (PR #8). On Backpack v4, either version works, but pin deliberately if you have a lockfile in CI.
  • First place to look is the README — it was corrected in this release to reflect the real view location (resources/views/vendor/backpack/crud/fields/toggle.blade.php). If you previously copied the blade file to the wrong path during a v4 → v5 upgrade, that's the fix.
  • First use case: a boolean column on a CRUD model, e.g. is_active. In your CrudController:
$this->crud->addField([
    'name'  => 'is_active',
    'label' => 'Active',
    'type'  => 'toggle',
]);
  • Pair it with a boolean column so the list view reads sensibly:
$this->crud->addColumn(['name' => 'is_active', 'type' => 'boolean']);
  • Migration side, keep it a real boolean: $table->boolean('is_active')->default(false);

Implementation Patterns

  • Define once, reuse everywhere. Put the toggle definition in a private method or trait and call it from both setupCreateOperation() and setupUpdateOperation() so the two forms never drift:
protected function addStatusFields(): void
{
    $this->crud->addField([
        'name'    => 'is_active',
        'type'    => 'toggle',
        'label'   => 'Active',
        'default' => false,
    ]);
}
  • Group inside fieldsets/tabs. Because the field is just a field type, it composes normally — drop it next to a select in a "Publishing" fieldset rather than giving it its own tab; a lone toggle in a tab looks broken to editors.
  • Inline create / modal forms. Backpack's inline-create flows re-render the same field views, so the toggle works there without changes — but set 'default' => false explicitly, otherwise the first render shows an ambiguous state.
  • Model casting is the real integration point. Add 'is_active' => 'boolean' to $casts. The field posts 0/1; without a cast you'll be comparing "0" strings in scopes and policies and getting surprises.
  • Validation. Use 'is_active' => ['boolean'] (not required) — a toggle that's off still submits a value, but required semantics get confusing when you later add nullable states.
  • Filtering. The package gives you the input, not the query layer. Add a scopeActive() on the model and a dropdown/select filter rather than trying to reuse the toggle as a filter.
  • Overriding look and feel. Publish the vendor view and edit the copy — never the vendor path — so composer update doesn't wipe your styling. This is the same view path the README now documents.

Gotchas and Tips

  • The classic unchecked-checkbox problem. A bare <input type="checkbox"> submits nothing when off. Backpack toggle fields normally pair the checkbox with a hidden 0 input; if you override the blade view, keep that hidden input or every "off" save will be interpreted as "leave unchanged" or fail validation.
  • View [toggle] not found. Almost always a v4/v5 path mismatch or a missing publish step. Confirm the blade file sits under resources/views/vendor/backpack/crud/fields/ (v5 location). This is exactly the confusion the README fix in 2.0.1 addresses.
  • Upgrade ordering matters. Bump Backpack to v5 and this package to 2.0.1 in the same change set. A half-upgraded pair is the most common source of "the field renders as a plain checkbox" reports.
  • Don't reach for it on non-boolean columns. Tri-state (null/0/1) and enum-backed flags don't map cleanly onto a two-position toggle; use a select with an explicit "unset" option instead.
  • Datatable exports. type => 'boolean' columns render nicely in the UI but often export as 1/0. If you need "Yes/No" in CSV exports, format in an exporter rather than fighting the column.
  • Debugging tip. When the toggle silently doesn't persist, check in order: request payload (dd($request->all()) in the store method — is is_active present at all?), the model's $fillable, then the cast. The field itself is rarely the culprit.
  • Version note. 2.0.0 → 2.0.1 contains no breaking changes for existing users — it's a gitignore addition, a README correction, and v5 support. The only action item is the Backpack v5 compatibility bump.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel
draftsmaninc/draftsman
square-bit/laravel-volition
himito/laravel-mailman
htinlynn/genius-helper
maco-studios/hephaestus
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log