PHP code example of idkwhoami / flux-tables

1. Go to this page and download the library: Download idkwhoami/flux-tables 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/ */

    

idkwhoami / flux-tables example snippets


$columns = [
    \Idkwhoami\FluxTables\Concretes\Column\TextColumn::make('name')
        ->label('Username')
        ->property('name')
        ->searchable()
        ->sortable(),
];

$filters = [
    \Idkwhoami\FluxTables\Concretes\Filter\DeletedFilter::make('deleted')
        ->label('Deleted')
        ->default(\Idkwhoami\FluxTables\Enums\DeletionState::WithoutDeleted->value),
];

class MyTable extends Component
{
    use HasEloquentTable;
    use HasSearch;
    use WithPagination;

    public function render(): View
    {
        return view('livewire.my-table');
    }

    #[Computed]
    public function models(): LengthAwarePaginator
    {
        return $this->getQuery()->paginate();
    }

    #[Computed]
    public function columns(): array
    {
        return [
            \Idkwhoami\FluxTables\Concretes\Column\TextColumn::make('name')
                ->label('Username')
                ->property('name')
                ->searchable()
                ->sortable(),
        ];
    }

    #[Computed]
    public function filters(): array
    {
        return [
            \Idkwhoami\FluxTables\Concretes\Filter\DeletedFilter::make('deleted')
                ->label('Deleted')
                ->default(\Idkwhoami\FluxTables\Enums\DeletionState::WithoutDeleted->value),
        ];
    }

    public function table(string $model, array $columns = [], array $filters = []): Table
    {
        return EloquentTable::make('mytable')
            ->model($model)
            ->columns($this->columns)
            ->filters($this->filters);
    }

    public function getQuery(): Builder
    {
        $sql =  $this->eloquentModel::query();

        $this->applySearch($sql);
        $this->applyRelations($sql);

        $sql->dumpRawSql();

        return $sql;
    }
}
bash
php artisan flux-tables:install
bladehtml
    @php
        <!-- PHP Code Above -->
    @endphp

    <livewire:flux-simple-table title="Users" :$columns :$filters :model="\App\Models\User::class"/>
bladehtml
<div class="flex w-full flex-col space-y-2">
    <div class="flex flex-col gap-y-2">
        <div class="flex gap-x-3">
            @if($this->table->hasLabel())
                <flux:heading class="content-center" level="1" size="xl">
                    {{ $this->table->getLabel() }}
                </flux:heading>
            @endif
            <flux:spacer/>
            <div class="w-42">
                <flux:input clearable size="sm" type="text" icon="search" wire:model.live.debounce="search"/>
            </div>
        </div>
    </div>

    <flux:table :paginate="$this->models">

        <flux:table.columns>
            @foreach($this->table->getColumns() as $column)
                <flux:table.column :key="$column->getName()">
                    {{ $column->getLabel() }}
                </flux:table.column>
            @endforeach
        </flux:table.columns>

        <flux:table.rows>
            @foreach($this->models as $model)
                <flux:table.row
                    wire:loading.class="animate-pulse"
                    :key="$model->getKey()">
                    @foreach($this->table->getColumns() as $column)
                        <flux:table.cell>
                            {{ $column->render($model) }}
                        </flux:table.cell>
                    @endforeach
                </flux:table.row>
            @endforeach
        </flux:table.rows>

    </flux:table>

</div>