Skip to content

Instantly share code, notes, and snippets.

@MatinMN
Created September 28, 2023 01:36
Show Gist options
  • Save MatinMN/383fd9180a77156279e4dbb8cc267941 to your computer and use it in GitHub Desktop.
Save MatinMN/383fd9180a77156279e4dbb8cc267941 to your computer and use it in GitHub Desktop.
Polymorphic relationship in Filament example 1-1 , 1-m
<?php
namespace App\Filament\Resources;
use App\Filament\Resources\CommentResource\Pages;
use App\Filament\Resources\CommentResource\RelationManagers;
use App\Filament\Resources\PostResource\RelationManagers\CommentsRelationManager;
use App\Models\Comment;
use App\Models\Post;
use App\Models\User;
use Filament\Forms;
use Filament\Forms\Components\MorphToSelect;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class CommentResource extends Resource
{
protected static ?string $model = Comment::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
public static function form(Form $form): Form
{
return $form
->schema([
Select::make('user_id')->relationship('user', 'name')->searchable()->preload(),
TextInput::make('comment'),
MorphToSelect::make('commentable')
->label("comment Type")
->types([
MorphToSelect\Type::make(Post::class)->titleAttribute('title'),
MorphToSelect\Type::make(User::class)->titleAttribute('email'),
MorphToSelect\Type::make(Comment::class)->titleAttribute('id'),
])
->searchable()
->preload()
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('user.name'),
TextColumn::make('commentable_type'),
TextColumn::make('commentable_id'),
TextColumn::make('comment'),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
])
->emptyStateActions([
Tables\Actions\CreateAction::make(),
]);
}
public static function getRelations(): array
{
return [
CommentsRelationManager::class,
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListComments::route('/'),
'create' => Pages\CreateComment::route('/create'),
'edit' => Pages\EditComment::route('/{record}/edit'),
];
}
}
<?php
namespace App\Filament\Resources\PostResource\RelationManagers;
use Filament\Forms;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class CommentsRelationManager extends RelationManager
{
protected static string $relationship = 'comments';
public function form(Form $form): Form
{
return $form
->schema([
Select::make('user_id')->relationship('user', 'name')->searchable()->preload(),
TextInput::make('comment'),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('comment')
->columns([
Tables\Columns\TextColumn::make('comment'),
Tables\Columns\TextColumn::make('user.name'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
])
->emptyStateActions([
Tables\Actions\CreateAction::make(),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment