Skip to content

Bulk actions

Adding bulk actions to your CRUD resource list views

INFO

Bulk actions are only available when using a resource manager for your list view.

Setup

Add the WithBulkActions trait to your resource manager class:

php
use Racerfish\Evolve\Concerns\Livewire\WithBulkActions;

class PostManager extends ResourceComponent
{
    // ...
    use WithBulkActions;

Then, pass the $selected variable to the selected prop of the resource list component in your index view:

html
<x-evolve::resource.list 
    :items="$posts" 
    :selected="$selected">
    <!-- ... -->
</x-evolve::resource.list>

Now add a checkbox input to your list items. The value must be your model's ID and wire:model must bind the selected Livewire property:

html
<input 
    type="checkbox" 
    name="selected[]" 
    value="{{ $post->id }}" 
    wire:model="selected">

Your list view is now ready for bulk actions.

Add custom actions

By default, only bulk deleting is available. But you can also define your own custom bulk actions.

Prepare the view

The x-evolve::resource.list provides a bulkActions slot to add dropdown items for your custom bulk actions:

html
<x-evolve::resource.list ...>
    <x-slot name="bulkActions">

        <button 
            class="dropdown__item" 
            type="button" 
            wire:click="publishSelected">
            Publish selected
        </button>

    </x-slot>

    <!-- ... -->
</x-evolve::resource.list>

Define the action logic

Create the Livewire action which will be called upon clicking the dropdown item, in our example publishSelected (because the button's wire:click binds it).

The WithBulkActions trait provides a method called getAllSelectedAsCursor() which returns all selected items as modal instances. You can loop through all of them to perform custom actions.

php
public function publishSelected()
{
    foreach ($this->getAllSelectedAsCursor() as $post) {
        $post->is_published = true;
        $post->save();
    }
}