Bulk Actions

Bulk Actions allow users to check off multiple records, choose an action, and have that action performed for all records at once. 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 $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:

{{ Form::bulkActionRowGroup('my-blog::general.posts', $bulk_actions, ['group' => 'my-blog', 'type' => 'posts']) }}

Add to vuejs

Finally, add the following line into the my-blog.js file:

bulk_action: new BulkAction('my-blog/posts')
Scroll to top