main

filamentphp/demo

Last updated at: 29/12/2023 09:48

CommentsRelationManager.php

TLDR

This file defines the CommentsRelationManager class. It extends the RelationManager class and provides methods to configure a form, infolist, and table for managing comments related to a product.

Methods

This file contains the following methods:

form

This method configures a form for managing comments. It sets up the form with columns, and then defines the form fields using Forms\Components classes such as TextInput, Select, Toggle, and MarkdownEditor. The form fields include the comment title, customer ID, visibility toggle, and comment content.

infolist

This method configures an infolist for displaying comments. It sets up the infolist with columns, and then defines the infolist entries using TextEntry and IconEntry classes. The infolist entries include the comment title, customer name, visibility icon, and comment content in markdown format.

table

This method configures a table for managing comments. It sets up the table columns using Tables\Columns classes such as TextColumn and IconColumn. The table columns include the comment title, customer name, and visibility icon. It also defines actions for the table, including creating a new comment, viewing, editing, and deleting comments, and bulk deleting comments.

Classes

This file contains the following class:

CommentsRelationManager

This class extends the RelationManager class and provides methods to configure a form, infolist, and table for managing comments related to a product. It defines the static property $relationship as 'comments' and the static property $recordTitleAttribute as 'title'.

<?php

namespace App\Filament\Resources\Shop\ProductResource\RelationManagers;

use Filament\Forms;
use Filament\Forms\Form;
use Filament\Infolists\Components\IconEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Infolists\Infolist;
use Filament\Notifications\Notification;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;

class CommentsRelationManager extends RelationManager
{
    protected static string $relationship = 'comments';

    protected static ?string $recordTitleAttribute = 'title';

    public function form(Form $form): Form
    {
        return $form
            ->columns(1)
            ->schema([
                Forms\Components\TextInput::make('title')
                    ->required(),

                Forms\Components\Select::make('customer_id')
                    ->relationship('customer', 'name')
                    ->searchable()
                    ->required(),

                Forms\Components\Toggle::make('is_visible')
                    ->label('Approved for public')
                    ->default(true),

                Forms\Components\MarkdownEditor::make('content')
                    ->required()
                    ->label('Content'),
            ]);
    }

    public function infolist(Infolist $infolist): Infolist
    {
        return $infolist
            ->columns(1)
            ->schema([
                TextEntry::make('title'),
                TextEntry::make('customer.name'),
                IconEntry::make('is_visible')
                    ->label('Visibility'),
                TextEntry::make('content')
                    ->markdown(),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('title')
                    ->label('Title')
                    ->searchable()
                    ->sortable(),

                Tables\Columns\TextColumn::make('customer.name')
                    ->label('Customer')
                    ->searchable()
                    ->sortable(),

                Tables\Columns\IconColumn::make('is_visible')
                    ->label('Visibility')
                    ->sortable(),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                Tables\Actions\CreateAction::make()
                    ->after(function ($record) {
                        Notification::make()
                            ->title('New comment')
                            ->icon('heroicon-o-chat-bubble-bottom-center-text')
                            ->body("**{$record->customer->name} commented on product ({$record->commentable->name}).**")
                            ->sendToDatabase(auth()->user());
                    }),
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->groupedBulkActions([
                Tables\Actions\DeleteBulkAction::make(),
            ]);
    }
}