Bulk Actions

Estimated reading: 2 minutes

Bulk Actions allow users to check off multiple records, choose an action, and perform that action for all records simultaneously. You can implement Bulk Actions to your module in just 3 steps:

Feel free to check out the My Blog module for the full example.

Create the class

<?php

namespace Modules\MyBlog\BulkActions;

use App\Abstracts\BulkAction;
use Modules\MyBlog\Exports\Posts as Export;
use Modules\MyBlog\Jobs\DeletePost;
use Modules\MyBlog\Models\Post;

class Posts extends BulkAction
{
    public $model = Post::class;

    public $text = 'my-blog::general.posts';

    public $path = [
        'group' => 'my-blog',
        'type' => 'posts',
    ];

    public $actions = [
        'enable' => [
            'name' => 'general.enable',
            'message' => 'bulk_actions.message.enable',
            'path' =>  ['group' => 'my-blog', 'type' => 'posts'],
            'permission' => 'update-my-blog-posts',
        ],
        'disable' => [
            'name' => 'general.disable',
            'message' => 'bulk_actions.message.disable',
            'path' =>  ['group' => 'my-blog', 'type' => 'posts'],
            'permission' => 'update-my-blog-posts',
        ],
        'delete' => [
            'name' => 'general.delete',
            'message' => 'bulk_actions.message.delete',
            'permission' => 'delete-my-blog-posts',
        ],
        'export' => [
            'name' => 'general.export',
            'message' => 'bulk_actions.message.export',
            'type' => 'download',
        ],
    ];

    public function destroy($request)
    {
        $posts = $this->getSelectedRecords($request, 'comments');

        foreach ($posts as $post) {
            try {
                $this->dispatch(new DeletePost($post));
            } catch (\Exception $e) {
                flash($e->getMessage())->error()->important();
            }
        }
    }

    public function export($request)
    {
        $selected = $this->getSelectedInput($request);

        return $this->exportExcel(new Export($selected), trans_choice('my-blog::general.posts', 2));
    }
}

Add to blade

Add the following line into the index.blade.php file:

<x-index.search
    search-string="Modules\MyBlog\Models\Post"
    bulk-action="Modules\MyBlog\BulkActions\Posts"
/>

Add to vuejs

Finally, add the following line into the posts.js file:

bulk_action: new BulkAction('posts')
Share this Doc

Bulk Actions

Or copy link

CONTENTS