Laravel Filament 3 Tabs Table Filters 学習メモ

Laravel Filament 3 Tabs Table Filters 学習メモ

続き

Filament 3 Tabs Table Filters サンプルコード

Forms\Components\Tabs

PostResource.php(変更)

<?php

namespace App\Filament\Resources;

use App\Filament\Resources\PostResource\Pages;
use App\Filament\Resources\PostResource\RelationManagers;
use App\Models\Category;
use App\Models\Post;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class PostResource extends Resource
{
    protected static ?string $model = Post::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                // Forms\Components\Section::make('Create a Post')
                //     ->description('create posts over here.')
                //     // ->aside()
                //     ->collapsible()
                //     ->schema([
                //         Forms\Components\TextInput::make('title')->required(),
                //         // Forms\Components\TextInput::make('title')->numeric()->minValue(3)->maxValue(10)->required(),
                //         Forms\Components\TextInput::make('slug')->unique(ignoreRecord:true)->required(),
                        
                //         Forms\Components\Select::make('category_id')
                //             ->label('Category')
                //             // ->multiple()
                //             // ->options(Category::all()->pluck('name','id'))
                //             ->relationship('category','name')
                //             // ->searchable()
                //             ->required(),
                //         Forms\Components\ColorPicker::make('color')->required(),
                        
                //         Forms\Components\MarkdownEditor::make('content')->required()->columnSpanFull(),
                // ])->columnSpan(2)->columns(2), //->columnSpanFull(),

                // Forms\Components\Group::make()->schema([
                //     Forms\Components\Section::make('Image')
                //         ->collapsible()
                //         ->schema([
                //             Forms\Components\FileUpload::make('thumbnail')->disk('public')->directory('thumbnails'),
                //         ])->columnSpan(1),
                //     Forms\Components\Section::make('Meta')
                //         ->schema([
                //             Forms\Components\TagsInput::make('tags')->required(),
                //             Forms\Components\Checkbox::make('published'),
                //         ]),
                //     // Forms\Components\Section::make('Authors')
                //     //     ->schema([
                //     //         // Forms\Components\Select::make('authors')
                //     //         Forms\Components\CheckboxList::make('authors')
                //     //         ->label('Go Authors')
                //     //         ->searchable()
                //     //         // ->multiple()
                //     //         ->relationship('authors', 'name')
                //     //     ]),
                // ]),
                // ])->columns([
                //     'default' => 1,
                //     'md' => 2,
                //     'lg' => 3,
                //     'xl' => 4,
                // ]);
    
                Forms\Components\Tabs::make('Create New Post')->tabs([
                    Forms\Components\Tabs\Tab::make('Tab 1')
                        ->icon('heroicon-m-inbox')
                        // ->iconPosition(Filament\Support\Enums\IconPosition::After)
                        ->badge('Hi')
                        ->schema([
                            Forms\Components\TextInput::make('title')->rules('min:3|max:10')->required(),
                            // Forms\Components\TextInput::make('title')->numeric()->minValue(3)->maxValue(10)->required(),
                            Forms\Components\TextInput::make('slug')->unique(ignoreRecord:true)->required(),
                            
                            Forms\Components\Select::make('category_id')
                                ->label('Category')
                                // ->multiple()
                                // ->options(Category::all()->pluck('name','id'))
                                ->relationship('category','name')
                                // ->searchable()
                                ->required(),
                            Forms\Components\ColorPicker::make('color')->required(),
                    ]),
                    Forms\Components\Tabs\Tab::make('Content')
                        ->icon('heroicon-o-rectangle-stack')
                        ->schema([
                            Forms\Components\MarkdownEditor::make('content')->required()->columnSpanFull(),
                    ]),
                    Forms\Components\Tabs\Tab::make('Meta')
                        ->icon('heroicon-o-rectangle-stack')
                        ->schema([
                            Forms\Components\FileUpload::make('thumbnail')->disk('public')->directory('thumbnails'),
                            Forms\Components\TagsInput::make('tags')->required(),
                            Forms\Components\Checkbox::make('published'),
                    ]),

                ])->columnSpanFull()->activeTab(3)->persistTabInQueryString(),  //アクティブ時のtab指定、tab選択時のurl表記

            ])->columns(3);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('id')
                ->sortable()
                ->searchable()
                ->toggleable(isToggledHiddenByDefault:true),
                Tables\Columns\ImageColumn::make('thumbnail')
                ->toggleable(),
                Tables\Columns\ColorColumn::make('color')
                ->toggleable(),
                Tables\Columns\TextColumn::make('title')
                ->sortable()
                ->searchable()
                ->toggleable(),
                Tables\Columns\TextColumn::make('slug')
                ->sortable()
                ->searchable()
                ->toggleable(),
                Tables\Columns\TextColumn::make('category.name')
                ->sortable()
                ->searchable()
                ->toggleable(),
                Tables\Columns\TextColumn::make('tags')
                ->toggleable(),
                Tables\Columns\CheckboxColumn::make('published')
                ->toggleable(),
                Tables\Columns\TextColumn::make('created_at')
                ->label('Published on')
                ->date()
                ->sortable()
                ->searchable()
                ->toggleable(),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            PostResource\RelationManagers\AuthorsRelationManager::class
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListPosts::route('/'),
            'create' => Pages\CreatePost::route('/create'),
            'edit' => Pages\EditPost::route('/{record}/edit'),
        ];
    }
}

Tables\Filters

PostResource.php(変更)

<?php

namespace App\Filament\Resources;

use App\Filament\Resources\PostResource\Pages;
use App\Filament\Resources\PostResource\RelationManagers;
use App\Models\Category;
use App\Models\Post;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class PostResource extends Resource
{
    protected static ?string $model = Post::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                // Forms\Components\Section::make('Create a Post')
                //     ->description('create posts over here.')
                //     // ->aside()
                //     ->collapsible()
                //     ->schema([
                //         Forms\Components\TextInput::make('title')->required(),
                //         // Forms\Components\TextInput::make('title')->numeric()->minValue(3)->maxValue(10)->required(),
                //         Forms\Components\TextInput::make('slug')->unique(ignoreRecord:true)->required(),
                        
                //         Forms\Components\Select::make('category_id')
                //             ->label('Category')
                //             // ->multiple()
                //             // ->options(Category::all()->pluck('name','id'))
                //             ->relationship('category','name')
                //             // ->searchable()
                //             ->required(),
                //         Forms\Components\ColorPicker::make('color')->required(),
                        
                //         Forms\Components\MarkdownEditor::make('content')->required()->columnSpanFull(),
                // ])->columnSpan(2)->columns(2), //->columnSpanFull(),

                // Forms\Components\Group::make()->schema([
                //     Forms\Components\Section::make('Image')
                //         ->collapsible()
                //         ->schema([
                //             Forms\Components\FileUpload::make('thumbnail')->disk('public')->directory('thumbnails'),
                //         ])->columnSpan(1),
                //     Forms\Components\Section::make('Meta')
                //         ->schema([
                //             Forms\Components\TagsInput::make('tags')->required(),
                //             Forms\Components\Checkbox::make('published'),
                //         ]),
                //     // Forms\Components\Section::make('Authors')
                //     //     ->schema([
                //     //         // Forms\Components\Select::make('authors')
                //     //         Forms\Components\CheckboxList::make('authors')
                //     //         ->label('Go Authors')
                //     //         ->searchable()
                //     //         // ->multiple()
                //     //         ->relationship('authors', 'name')
                //     //     ]),
                // ]),
                // ])->columns([
                //     'default' => 1,
                //     'md' => 2,
                //     'lg' => 3,
                //     'xl' => 4,
                // ]);
    
                Forms\Components\Tabs::make('Create New Post')->tabs([
                    Forms\Components\Tabs\Tab::make('Tab 1')
                        ->icon('heroicon-m-inbox')
                        // ->iconPosition(Filament\Support\Enums\IconPosition::After)
                        ->badge('Hi')
                        ->schema([
                            Forms\Components\TextInput::make('title')->rules('min:3|max:10')->required(),
                            // Forms\Components\TextInput::make('title')->numeric()->minValue(3)->maxValue(10)->required(),
                            Forms\Components\TextInput::make('slug')->unique(ignoreRecord:true)->required(),
                            
                            Forms\Components\Select::make('category_id')
                                ->label('Category')
                                // ->multiple()
                                // ->options(Category::all()->pluck('name','id'))
                                ->relationship('category','name')
                                // ->searchable()
                                ->required(),
                            Forms\Components\ColorPicker::make('color')->required(),
                    ]),
                    Forms\Components\Tabs\Tab::make('Content')
                        ->icon('heroicon-o-rectangle-stack')
                        ->schema([
                            Forms\Components\MarkdownEditor::make('content')->required()->columnSpanFull(),
                    ]),
                    Forms\Components\Tabs\Tab::make('Meta')
                        ->icon('heroicon-o-rectangle-stack')
                        ->schema([
                            Forms\Components\FileUpload::make('thumbnail')->disk('public')->directory('thumbnails'),
                            Forms\Components\TagsInput::make('tags')->required(),
                            Forms\Components\Checkbox::make('published'),
                    ]),

                ])->columnSpanFull()->activeTab(3)->persistTabInQueryString(),  //アクティブ時のtab指定、tab選択時のurl表記

            ])->columns(3);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('id')
                ->sortable()
                ->searchable()
                ->toggleable(isToggledHiddenByDefault:true),
                Tables\Columns\ImageColumn::make('thumbnail')
                ->toggleable(),
                Tables\Columns\ColorColumn::make('color')
                ->toggleable(),
                Tables\Columns\TextColumn::make('title')
                ->sortable()
                ->searchable()
                ->toggleable(),
                Tables\Columns\TextColumn::make('slug')
                ->sortable()
                ->searchable()
                ->toggleable(),
                Tables\Columns\TextColumn::make('category.name')
                ->sortable()
                ->searchable()
                ->toggleable(),
                Tables\Columns\TextColumn::make('tags')
                ->toggleable(),
                Tables\Columns\CheckboxColumn::make('published')
                ->toggleable(),
                Tables\Columns\TextColumn::make('created_at')
                ->label('Published on')
                ->date()
                ->sortable()
                ->searchable()
                ->toggleable(),
            ])
            ->filters([
                // Tables\Filters\Filter::make('Published Posts')->query(
                //     function($query) {
                //         return $query->where('published', true);
                //     }
                // )
                // Tables\Filters\Filter::make('Published Posts')->query(
                //     function(Builder $query): Builder {
                //         return $query->where('published', true);
                //     }
                // ),
                // Tables\Filters\Filter::make('UnPublished Posts')->query(
                //     function(Builder $query): Builder {
                //         return $query->where('published', false);
                //     }
                // ),
                Tables\Filters\TernaryFilter::make('published'),
                Tables\Filters\SelectFilter::make('category_id')
                    ->label('Category')
                    ->relationship('category', 'name')
                    // ->options(Category::all()->pluck('name', 'id'))
                    ->searchable()
                    ->preload()
                    ->multiple()
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            PostResource\RelationManagers\AuthorsRelationManager::class
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListPosts::route('/'),
            'create' => Pages\CreatePost::route('/create'),
            'edit' => Pages\EditPost::route('/{record}/edit'),
        ];
    }
}