PHP code example of manusiakemos / laravel-tanstack

1. Go to this page and download the library: Download manusiakemos/laravel-tanstack 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/ */

    

manusiakemos / laravel-tanstack example snippets


use Manusiakemos\LaravelTanstack\DataTable;
use App\Models\User;

class UserDataTableController
{
    public function __invoke(Request $request)
    {
        return DataTable::for(User::query())
            ->searchable(['name', 'email'])
            ->sortable(['name', 'email', 'created_at'])
            ->filterable(['status', 'role']);
    }
}

Route::get('/datatable/users', UserDataTableController::class)->middleware('auth');

DataTable::for(User::query()->with('role'))
    ->transform(fn ($user) => [
        'id' => $user->id,
        'name' => $user->name,
        'role' => $user->role->name,
        'status_label' => $user->status === 'active' ? 'Active' : 'Inactive',
    ]);

DataTable::for(User::query())->resource(UserResource::class);

DataTable::for(User::query())
    ->search(fn ($q, $term) =>
        $q->whereRaw('LOWER(name) LIKE ?', ['%'.strtolower($term).'%'])
          ->orWhereHas('role', fn ($r) => $r->where('name', 'like', "%{$term}%"))
    );

DataTable::for(User::query())
    ->sortable(['name', 'role_name'])
    ->orderColumn('role_name', fn ($q, $dir) =>
        $q->orderBy(
            Role::select('name')->whereColumn('roles.id', 'users.role_id'),
            $dir
        )
    );

DataTable::for(User::query())
    ->filterColumn('created_between', fn ($q, $value) => 
        $q->whereBetween('created_at', explode(',', $value))
    );

DataTable::for(User::query())
    ->authorize(fn () => Gate::allows('viewAny', User::class));

DataTable::for(User::query())
    ->sortable(['created_at'])
    ->defaultSort('created_at', 'desc');

DataTable::for(User::query())->skipTotal();

DataTable::for(User::query())
    ->defaultPerPage(50)
    ->maxPerPage(200);

return User::query()
    ->where('active', true)
    ->toDataTable()
    ->searchable(['name'])
    ->sortable(['name']);

return [
    'default_per_page' => 25,
    'max_per_page' => 100,
    'case_insensitive' => true,
    'report_exceptions' => true,
];
bash
php artisan vendor:publish --tag=laravel-tanstack-config