main

filamentphp/demo

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

ManagePostComments.php

TLDR

The ManagePostComments file is a PHP class that extends the ManageRelatedRecords class. It is part of the Demo Projects project and is located at app/Filament/Resources/Blog/PostResource/Pages/ManagePostComments.php. The file provides functionality for managing comments on a post in a blog.

Methods

getTitle(): string | Htmlable

This method returns the title for the page that manages post comments.

getBreadcrumb(): string

This method returns the breadcrumb label for the page that manages post comments.

getNavigationLabel(): string

This static method returns the navigation label for the page that manages post comments.

form(Form $form): Form

This method configures and returns a form for managing post comments. The form includes input fields for the comment's title, customer ID, visibility, and content.

infolist(Infolist $infolist): Infolist

This method configures and returns an infolist for displaying post comments. The infolist includes columns for the comment's title, customer name, visibility icon, and content.

table(Table $table): Table

This method configures and returns a table for displaying post comments. The table includes columns for the comment's title, customer name, and visibility icon. It also includes actions for creating, viewing, editing, and deleting comments, as well as bulk actions for deleting multiple comments.

Classes

ManagePostComments

This class extends the ManageRelatedRecords class and provides functionality for managing comments on a post in a blog. It includes methods for configuring and returning a form, infolist, and table for managing and displaying post comments.

<?php

namespace App\Filament\Resources\Blog\PostResource\Pages;

use App\Filament\Resources\Blog\PostResource;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Infolists\Components\IconEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Infolists\Infolist;
use Filament\Resources\Pages\ManageRelatedRecords;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Contracts\Support\Htmlable;

class ManagePostComments extends ManageRelatedRecords
{
    protected static string $resource = PostResource::class;

    protected static string $relationship = 'comments';

    protected static ?string $navigationIcon = 'heroicon-o-chat-bubble-left-ellipsis';

    public function getTitle(): string | Htmlable
    {
        return "Manage {$this->getRecordTitle()} Comments";
    }

    public function getBreadcrumb(): string
    {
        return 'Comments';
    }

    public static function getNavigationLabel(): string
    {
        return 'Manage Comments';
    }

    public function form(Form $form): Form
    {
        return $form
            ->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'),
            ])
            ->columns(1);
    }

    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
            ->recordTitleAttribute('title')
            ->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(),
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->groupedBulkActions([
                Tables\Actions\DeleteBulkAction::make(),
            ]);
    }
}