🤹
TALL Stack Tips
  • What is TALL Stack
  • Tailwind
    • Tailwind Resources
    • Swinging Bell Notification Icon
    • Styled Unordered Lists
  • Alpine
  • Alpine Resources
  • Tabbed Content Using Alpine JS
  • Checkbox component with SVG tick
  • Dropdown animation
  • Create a Sliding Puzzle Captcha
  • Laravel
    • Tabler Icons Component
    • Password-less Login with Laravel 8+
    • Password-less Login with Magic Link in Laravel 8
    • Laravel Resources
    • Laravel Breeze Login Conditional Redirect
    • Jetstream Login Conditional Redirect
    • Simplify Laravel CRUD Controllers
    • CSRF and expired login forms
    • CSRF and expired logout forms
    • Add your own logo to Laravel Mail
    • Specify a different mail theme for Notifications
    • Show custom page when email verification link expired
    • Using a mutator to save currency
    • Using Spatie Valuestore to hold frequently accessed settings
    • Using the old() helper
    • Alternatives to using Eloquent Accessor
    • UpdateOrCreate may not update timestamp
    • Use of lockForUpdate()
    • Using S3
    • Super Simple User Based Feature Flags
    • Installing a Specific Version of Laravel
    • Versioning your Laravel Project
    • CSS Cache Busting with your Git Commit SHA
    • Adding column to Database Notifications table
    • Find nearby locations using the Haversine formula in Eloquent query
    • Using Queues on Shared Hosting with Laravel
    • Create Guaranteed Unique Invoice Number in Laravel
    • Send Notification to all team members
    • Protect Staging site with Basic Auth
    • Working with Enums
    • PHP DateTime formatting cribsheet
  • Livewire
    • Livewire Resources
    • Naming Livewire Components
    • Dynamic Cascading Dropdown with Livewire
    • Hiding a button after click
    • Working with Javascript Components
    • SweetAlert2 with Livewire
    • Select Multiple or Checkboxes
    • Clearing checkboxes in Livewire
    • Livewire File Uploads Using S3
    • Simple Log File Viewer
  • Related Resources
    • Testing resources
    • When Composer runs out of memory
    • Deployment
    • Security
    • Scheduler & Cron tips
    • LastPass tips
    • Using Git
    • VSCode Tips
    • Markdown
    • Cpanel resources
Powered by GitBook
On this page

Was this helpful?

  1. Livewire

Clearing checkboxes in Livewire

When setting empty array does not clear checkboxes

I encountered an issue where checkboxes are not cleared when the array they are bound to is cleared.

Checkbox input elements like this;

<input wire:model="recipients.{{ $contact->id }}" 
    type="checkbox" 
    name="recipient-{{ $contact->id }}" 
    id="recipient-{{ $contact->id }}"
>

the array of recipients uses dotted notation to specify the index

Recipients are contacts in the system and are output as an array for the user to select a number of recipients and then perform the action. As each recipient is checked a key/value pair is added to the array.

array:2 [
  42 => true
  34 => true
]

Checkboxes that have not been selected are not present in the array. Selecting then deselecting a checkbox causes its entry to be set false;

array:2 [
  42 => false
  34 => true
]

Once the user has selected a number of recipients and then performed the activity, the checkboxes should be cleared. Setting the $recipients to an empty array does nothing to the view. The previously checked checkboxes are still checked following render.

The solution to clear the checkboxes is to iterate through the array in the Livewire component, setting recipients to false;

foreach($this->recipients as &$recipient) {
    $recipient = false;
}

note the use of & to pass the array entry by reference.

PreviousSelect Multiple or CheckboxesNextLivewire File Uploads Using S3

Last updated 4 years ago

Was this helpful?