Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Filament Relation Manager Component Laravel Package

njxqlus/filament-relation-manager-component

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require njxqlus/filament-relation-manager-component
    

    Publish views (optional, for customization):

    php artisan vendor:publish --tag="filament-relation-manager-component-views"
    
  2. First Use Case: Integrate into a Filament resource's form() or table() method. Example for a Post resource managing Comments:

    use Njxqlus\Filament\Components\Forms\RelationManager;
    
    public static function form(Schema $schema): Schema {
        return $schema->schema([
            RelationManager::make('comments')
                ->relationship('comments') // Define the relationship
                ->manager(CommentsRelationManager::class) // Custom manager class
                ->lazy(false), // Load immediately (not lazy-loaded)
        ]);
    }
    
  3. Key Files to Review:

    • app/Filament/Resources/[ResourceName]/Pages/EditPost.php (for form integration).
    • app/Filament/RelationManagers/CommentsRelationManager.php (custom manager logic).
    • vendor/njxqlus/filament-relation-manager-component/src/Components/ (core components).

Implementation Patterns

Core Workflows

  1. Basic Integration:

    • Use RelationManager::make() in form() or table() methods.
    • Specify the relationship name (e.g., ->relationship('comments')).
    • Attach a custom RelationManager class (extends Filament\Forms\Components\RelationManager).
  2. Tabbed Layouts: Group multiple relation managers in tabs for organized UX:

    Schemas\Components\Tabs::make()->tabs([
        Schemas\Components\Tabs\Tab::make('Comments')->schema([
            RelationManager::make()->manager(CommentsRelationManager::class),
        ]),
        Schemas\Components\Tabs\Tab::make('Tags')->schema([
            RelationManager::make()->manager(TagsRelationManager::class),
        ]),
    ]);
    
  3. Custom Managers: Extend Filament\Forms\Components\RelationManager to override default behavior:

    namespace App\Filament\RelationManagers;
    
    use Filament\Forms;
    use Filament\Forms\Form;
    use Filament\Tables;
    
    class CommentsRelationManager extends \Filament\Forms\Components\RelationManager {
        protected static ?string $model = Comment::class;
    
        public function form(Form $form): Form {
            return $form
                ->schema([
                    Forms\Components\TextInput::make('body')->required(),
                    Forms\Components\Select::make('status')->options(['draft', 'published']),
                ]);
        }
    }
    
  4. Infolist Integration: Display read-only relations in infolist():

    public static function infolist(Schema $schema): Schema {
        return $schema->schema([
            RelationManager::make('comments')
                ->relationship('comments')
                ->manager(CommentsRelationManager::class)
                ->lazy(false),
        ]);
    }
    
  5. Modal/Slideover Support: Use ->modal() or ->slideover() for inline editing:

    RelationManager::make()
        ->manager(CommentsRelationManager::class)
        ->modal()
        ->slideover();
    

Advanced Patterns

  • Dynamic Relationships: Use closures to define relationships dynamically:

    RelationManager::make()
        ->relationship(fn ($record) => $record->currentTeam()->comments())
    
  • Conditional Rendering: Show/hide relation managers based on logic:

    RelationManager::make()
        ->manager(CommentsRelationManager::class)
        ->visible(fn ($record) => $record->isApproved())
    
  • Bulk Actions: Extend the manager to support bulk operations:

    class CommentsRelationManager extends \Filament\Forms\Components\RelationManager {
        public function table(Tables\Table $table): Tables\Table {
            return $table
                ->columns([
                    Tables\Columns\TextColumn::make('body'),
                ])
                ->actions([
                    Tables\Actions\DeleteAction::make(),
                    Tables\Actions\EditAction::make(),
                ])
                ->bulkActions([
                    Tables\Actions\BulkAction::make('approve')
                        ->action(fn (Collection $records) => $records->update(['status' => 'approved']))
                ]);
        }
    }
    

Gotchas and Tips

Common Pitfalls

  1. Lazy Loading:

    • Default behavior is lazy-loaded (lazy(true)). Set lazy(false) for immediate rendering (e.g., in tabs).
    • Fix: Always explicitly set lazy() to avoid unexpected delays.
  2. Owner Record Assignment:

    • Relations may fail if the ownerRecord isn’t properly passed (e.g., in modals/slideovers).
    • Fix: Ensure the custom manager’s getOwnerRecord() method is implemented:
      public static function getOwnerRecord(): ?Model {
          return parent::getOwnerRecord() ?? request()->filamentResourceRecord;
      }
      
  3. Filament Version Mismatches:

    • The package supports Filament v3–v5. Check the composer.json for required versions.
    • Fix: Use the correct namespace (e.g., \Filament\Forms\Components\RelationManager for v3, \Filament\Forms\Components\RelationManager for v4+).
  4. Published Views:

    • Customizing views requires publishing assets. Override the default blade templates in resources/views/vendor/filament-relation-manager-component/.
    • Tip: Use php artisan vendor:publish --tag="filament-relation-manager-component-views" and modify the published files.
  5. Performance with Large Datasets:

    • Lazy loading helps, but complex queries may still slow down the UI.
    • Tip: Use ->query() to scope relations:
      RelationManager::make()
          ->relationship('comments')
          ->query(fn ($query) => $query->where('status', 'published'))
      

Debugging Tips

  • Check the Owner Record: Add a dd() in the manager’s getOwnerRecord() to verify the parent model is passed correctly.

  • Log Queries: Enable Laravel’s query logging to debug relation queries:

    DB::enableQueryLog();
    // ... perform action ...
    dd(DB::getQueryLog());
    
  • Inspect the Schema: Use Schema::make()->toHtml() to debug the rendered schema structure.

Extension Points

  1. Custom Table/Form Components: Override the table or form in the manager class:

    public function table(Tables\Table $table): Tables\Table {
        return $table->columns([...]);
    }
    
  2. Event Hooks: Listen to Filament’s events (e.g., RelationManagerSaved) for post-save logic:

    event(new RelationManagerSaved($this, $record));
    
  3. API Integration: Extend the manager to support API-only workflows by implementing getApiResource():

    public function getApiResource(): ?string {
        return \App\Filament\Resources\CommentResource::class;
    }
    
  4. Localization: Override labels and messages in the manager:

    public static ?string $label = 'Customer Reviews';
    public static ?string $pluralLabel = 'Customer Reviews';
    

Pro Tips

  • Reuse Managers Across Resources: Create a base manager class and extend it for shared logic:

    class BaseRelationManager extends \Filament\Forms\Components\RelationManager {
        public function getTitle(): string {
            return $this->getOwnerRecord()->name . ' Relations';
        }
    }
    
  • Use hasManyThrough: Support complex relationships like hasManyThrough:

    RelationManager::make()
        ->relationship('posts')
        ->manager(PostsRelationManager::class)
        ->query(fn ($query) => $query->whereHas('author', fn ($q) => $q->where('user_id', $this->getOwnerRecord()->id)))
    
  • Testing: Test relation managers with Filament’s testing helpers:

    $this->actingAsUser($user)
         ->get('/admin/resources/posts/1/edit')
         ->assertSee('Relation Manager Title');
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor