1. Go to this page and download the library: Download power-components/turbine 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/ */
// routes/web.php or routes/api.php
Route::get('/users/grid', UserGridController::class);
use PowerComponents\Turbine\{GridDefinition, Column, Fields, Turbine};
class UsersGrid extends GridDefinition
{
public string $tableName = 'users';
public int $perPage = 10;
public function datasource(): mixed
{
return User::query();
}
public function fields(): Fields
{
return Fields::make()->add('id')->add('name')->add('email');
}
public function columns(): array
{
return [
Column::make('ID', 'id')->sortable(),
Column::make('Name', 'name')->searchable()->sortable(),
];
}
public function setUp(): array
{
return [Turbine::footer()->showPerPage(10, [10, 25, 50])];
}
}
public function __invoke(Request $request)
{
$grid = new UsersGrid();
return Inertia::render('users', [
'columns' => fn () => $grid->columns(),
'grid' => fn () => $grid->envelope($request),
]);
}
Button::add('edit')->slot('Edit')->route('users.edit', ['user' => $user->id]);
Button::add('delete')->slot('Delete')->dispatch('deleteUser', ['id' => $user->id])->confirm('Are you sure?');
use PowerComponents\Turbine\Components\Rules\{RuleActions, RuleRows, RuleCheckbox, RuleToggleable};
Turbine::make()
->actionRules(fn (User $user) => [
// Hide delete action for admins
(new RuleActions('delete'))->when(fn ($u) => $u->is_admin)->hide(),
// Highlight admin rows
(new RuleRows())->when(fn ($u) => $u->is_admin)->setAttribute('class', 'bg-blue-50'),
// Disable checkboxes for system users
(new RuleCheckbox())->when(fn ($u) => $u->is_system)->disable(),
]);
// Standard filter usage
new FilterSelect('status')->dataSource(UserStatusEnum::cases());
// Custom query logic callback
new FilterInputText('title')->builder(fn ($query, $value) => $query->whereRaw('LOWER(title) LIKE ?', ["%{$value}%"]));
// Eloquent Query Builder or Model
Turbine::make()->datasource(fn () => User::query());
// Laravel Scout
Turbine::make()->datasource(fn () => User::search($term));
// Array or Collection
Turbine::make()->datasource(fn () => collect([['id' => 1, 'name' => 'Alice']]));
use PowerComponents\Turbine\Contracts\DataSourceProcessor;
use PowerComponents\Turbine\DataSource\Processors\DataSourceBase;
class CustomApiProcessor extends DataSourceBase implements DataSourceProcessor
{
public static function match(mixed $datasource): bool => $datasource instanceof MyCustomClient;
public function process(array $properties = [], mixed $datasource = null): array
{
// Fetch data, return length-aware paginator in results
return ['results' => $paginator, 'actionsByRow' => []];
}
}
// Register globally
Turbine::registerDataSource(CustomApiProcessor::class);
use PowerComponents\Turbine\Export\ExportEngine;
$filePath = app(ExportEngine::class)->build(
context: $turbine->context(),
exportType: 'xlsx', // 'xlsx' or 'csv'
fileName: 'users_export'
);
return response()->download($filePath)->deleteFileAfterSend(true);
use PowerComponents\Turbine\Support\State\StatePersister;
$persister = new StatePersister();
// Save state to Cookie, Session, or Cache
$persister->serializeState(['columns', 'filters', 'sorting'], 'users', $stateArray);
// Restore saved state
$savedState = $persister->getPersistedState('users');
return (new UserTable())->toDataResponse($request);
use PowerComponents\Turbine\Response;
use PowerComponents\Turbine\Support\State\{ArrayGridContext, State};
$context = new ArrayGridContext(
state: State::fromRequest($request),
datasourceResolver: fn () => User::query(),
fields: $fields,
columns: $columns,
);
/** @var \PowerComponents\Turbine\Response\GridResponse $envelope */
$envelope = Response::make($context)->envelope();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.