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 (if customization is needed):

    php artisan vendor:publish --provider="Njxqlus\FilamentRelationManager\ComponentServiceProvider"
    
  2. First Use Case: Register the component in a Filament resource's getPages() method:

    use Njxqlus\FilamentRelationManager\Component;
    
    public static function getPages(): array
    {
        return [
            Component::make('posts', 'Posts'),
        ];
    }
    
  3. Basic Configuration: Define relations in the model (e.g., Post):

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
    

    Access the relation manager in the resource:

    public static function getRelations(): array
    {
        return [
            'comments',
        ];
    }
    

Implementation Patterns

Core Workflows

  1. Relation Management:

    • Use the component to manage polymorphic or complex relations (e.g., morphToMany, hasManyThrough).
    • Example for polymorphic relations:
      public function tags()
      {
          return $this->morphToMany(Tag::class, 'taggable');
      }
      
      Register in resource:
      public static function getRelations(): array
      {
          return [
              'tags',
          ];
      }
      
  2. Customizing UI:

    • Override views (published via vendor:publish) to modify forms/tables.
    • Extend the component class for custom logic:
      use Njxqlus\FilamentRelationManager\Component as BaseComponent;
      
      class CustomRelationManager extends BaseComponent
      {
          protected static ?string $navigationIcon = 'heroicon-o-collection';
      }
      
  3. Integration with Filament Features:

    • Combine with Filament’s Table or Form components for nested CRUD:
      Component::make('comments')
          ->table(CommentTable::class)
          ->form(CommentForm::class),
      
    • Use relationManager() helper in resource actions:
      public static function getTableActions(): array
      {
          return [
              Tables\Actions\Action::make('manageRelations')
                  ->action(function (Post $record) {
                      return RelationManager::make('posts', 'Posts')
                          ->record($record)
                          ->open();
                  }),
          ];
      }
      
  4. Dynamic Relations:

    • Load relations conditionally based on model attributes:
      public static function getRelations(): array
      {
          return fn (Post $record) => $record->isPublished ? ['comments', 'tags'] : ['draftComments'];
      }
      

Gotchas and Tips

Common Pitfalls

  1. Relation Not Found:

    • Ensure the relation method exists in the model and is listed in getRelations().
    • Debug with php artisan tinker:
      $post = Post::first();
      $post->comments; // Verify relation works in Tinker.
      
  2. Permission Issues:

    • The component inherits Filament’s authorization. Use canAccessRelationManager() in resources:
      public static function canAccessRelationManager(): bool
      {
          return auth()->user()->can('manage-post-relations');
      }
      
  3. Performance:

    • Eager-load relations in the resource’s getRecords() to avoid N+1 queries:
      public static function getRecords(Table $table): array
      {
          return Post::with(['comments', 'tags'])->get();
      }
      
  4. View Publishing:

    • Always publish views before customizing them to avoid overwrites:
      php artisan vendor:publish --tag="filament-relation-manager-views"
      

Debugging Tips

  • Log Relation Data: Override getRelationData() in a custom component:

    protected function getRelationData(string $relation): array
    {
        \Log::info("Fetching relation: {$relation}", ['data' => $this->record->$relation]);
        return parent::getRelationData($relation);
    }
    
  • Check Component Registration: Verify the component is registered in getPages() and getRelations():

    // Resource.php
    public static function getPages(): array
    {
        return [
            Component::make('posts', 'Posts'), // Must match relation name.
        ];
    }
    

Extension Points

  1. Custom Relation Managers: Extend the base component for reusable logic:

    class MediaRelationManager extends Component
    {
        protected static string $model = Media::class;
        protected static ?string $navigationGroup = 'Media';
    }
    
  2. Hooks: Use Filament’s hooks to modify behavior:

    Filament::registerResourceHooks(function (ResourceHooks $resourceHooks) {
        $resourceHooks->register('relation-manager.created', function (Post $record, string $relation) {
            event(new RelationCreated($record, $relation));
        });
    });
    
  3. Localization: Translate relation labels dynamically:

    Component::make('comments')
        ->label(fn () => __("filament-relation-manager::relations.comments")),
    
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui