PHP code example of manggala / laravel-datatable

1. Go to this page and download the library: Download manggala/laravel-datatable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

manggala / laravel-datatable example snippets


namespace App\Tables;

use App\Models\User;
use Manggala\DataTable\Core\DataTable;
use Manggala\DataTable\Columns\TextColumn;
use Manggala\DataTable\Columns\BadgeColumn;
use Manggala\DataTable\Columns\AvatarColumn;
use Manggala\DataTable\Columns\DateColumn;
use Manggala\DataTable\Columns\ActionColumn;
use Manggala\DataTable\Filters\SelectFilter;
use Manggala\DataTable\Filters\DateRangeFilter;
use Manggala\DataTable\Actions\BulkAction;

class UsersTable extends DataTable
{
    public function query()
    {
        return User::query()->with('roles');
    }

    public function columns(): array
    {
        return [
            AvatarColumn::make('avatar_url')->label('')->size('sm'),
            TextColumn::make('name')->label('Full Name')->sortable()->searchable()->copyable(),
            TextColumn::make('email')->label('Email Address')->sortable()->searchable(),
            BadgeColumn::make('role')->label('Role')
                ->colors([
                    'admin' => 'red',
                    'editor' => 'yellow',
                    'user' => 'blue',
                ]),
            DateColumn::make('created_at')->label('Joined Date')->format('M d, Y')->sortable(),
            ActionColumn::make('actions')->label('Actions'),
        ];
    }

    public function filters(): array
    {
        return [
            SelectFilter::make('role')->options([
                'admin' => 'Administrator',
                'editor' => 'Editor',
                'user' => 'Regular User',
            ]),
            DateRangeFilter::make('created_at')->label('Registration Date'),
        ];
    }

    public function bulkActions(): array
    {
        return [
            BulkAction::make('delete')
                ->label('Delete Selected')
                ->icon('trash')
                ->danger()
                ->confirm('Are you sure you want to delete selected users?')
                ->action(fn ($ids) => User::destroy($ids)),
        ];
    }
}

namespace App\Http\Controllers;

use App\Tables\UsersTable;
use Illuminate\Http\Request;
use Inertia\Inertia;

class UserController extends Controller
{
    public function index(Request $request)
    {
        return Inertia::render('Users/Index', [
            'usersTable' => UsersTable::make()->render($request),
        ]);
    }
}

// Protect bulk action using Laravel Gate
BulkAction::make('delete')
    ->can('delete-users')
    ->action(fn ($ids) => User::destroy($ids));

// Protect column based on custom closure condition
TextColumn::make('salary')
    ->when(fn ($user) => $user->isAdmin());

return [
    'per_page' => 15,
    'per_page_options' => [10, 15, 25, 50, 100],
    'search_debounce_ms' => 200,
    'default_density' => 'normal', // 'compact', 'normal', 'comfortable'
    'export' => [
        'chunk_size' => 1000,
    ],
];
bash
php artisan datatable:install
bash
# Publish configuration file
php artisan datatable:publish --tag=config

# Publish React component views
php artisan datatable:publish --tag=views