PHP code example of asignua / filament-spreadsheet-grid

1. Go to this page and download the library: Download asignua/filament-spreadsheet-grid 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/ */

    

asignua / filament-spreadsheet-grid example snippets


use Asignua\FilamentSpreadsheetGrid\Columns\GridColumn;
use Asignua\FilamentSpreadsheetGrid\Concerns\InteractsWithSpreadsheetGrid;
use Asignua\FilamentSpreadsheetGrid\SpreadsheetGrid;
use Filament\Resources\Pages\ListRecords;

class ListProducts extends ListRecords
{
    use InteractsWithSpreadsheetGrid;

    protected static string $resource = ProductResource::class;
}

// ProductResource
public static function table(Table $table): Table
{
    return $table
        ->header(SpreadsheetGrid::toolbar())
        ->columns([
            TextColumn::make('id')->alignEnd(),
            GridColumn::make('name')->

GridColumn::make('title')->>number(min: 0, max: 99999)->rules(['decimal:0,2']);
GridColumn::make('qty')->integer(min: 0);
GridColumn::make('status')->select(['draft' => 'Draft', 'live' => 'Live']);   // or select(StatusEnum::class)
GridColumn::make('starts_on')->date('d.m.Y');   // shown as d.m.Y, stored and copied as Y-m-d (DATE columns only)
GridColumn::make('is_active')->boolean();
GridColumn::make('sku')->rules(fn (Product $record) => [Rule::unique('products', 'sku')->ignore($record)]);
GridColumn::make('code')->editable(fn (Product $record) => ! $record->locked);
GridColumn::make('cost')->number()->visible(fn () => auth()->user()->isAdmin());   // hidden = not sent, not writable

$record->setAttribute($column, $value); // for every changed cell
$record->save();

class ListProducts extends ListRecords
{
    use InteractsWithSpreadsheetGrid;

    protected function spreadsheetGrid(SpreadsheetGrid $grid): SpreadsheetGrid
    {
        return $grid
            ->saveUsing(fn (Product $record, array $changes) => app(ProductRepository::class)->update($record, ProductData::fromArray($changes)))
            ->autosave()      // save each commit at once (the user can switch it off; autosave(toggle: false) forbids that)
            ->atomic();       // any failed row => nothing is written
    }
}

protected function spreadsheetGrid(SpreadsheetGrid $grid): SpreadsheetGrid
{
    return $grid->toggleable(default: false, persist: false);
}

$grid->authorizeUsing(fn (Product $record): bool => auth()->user()->can('update', $record));