filament-drafts icon indicating copy to clipboard operation
filament-drafts copied to clipboard

Trait Method Collision Error

Open ZayRTun opened this issue 1 year ago • 3 comments

Description: After adding the traits to all the resource pages I am getting the following error.

Note: It's happening because of Filament Translatable plugin.

Trait method Guava\FilamentDrafts\Admin\Resources\Pages\Create\Draftable::handleRecordCreation has not been applied as App\Filament\Resources\DeveloperResource\Pages\CreateDeveloper::handleRecordCreation, because of collision with Filament\Resources\Pages\CreateRecord\Concerns\Translatable::handleRecordCreation

Environment:

  • PHP version: 8.2
  • Laravel version: 11.14
  • Filament version: 3.2

Possible Solution: To resolve the collision, consider renaming one of the conflicting methods..

Screenshot 2024-07-10 at 09 57 51

ZayRTun avatar Jul 10 '24 03:07 ZayRTun

I've just come across almost this exact issue, while trying to add this package to an app that uses Translatable. The issue is that both traits are attempting to overwrite the base handleRecordCreation() method from the CreateRecord class, so renaming the method won't work. Short term, options are:

  1. create your own handleRecordCreation() method that does the things you want from both traits; (probably the best approach for a single use-case).
  2. see if there's a different approach to using the underlying OddType\Draftable package without changing handleRecordCreation().

I wonder if, long term, the idea of overwriting the handleRecordCreation method to modify how records are saved with traits isn't the best approach for either package.

dave-mills avatar Sep 18 '24 15:09 dave-mills

A possible fix would be to provide our own Translatable implementation just like we do in Nested Resources, but then if these two packages would be used together, a collision will still happen.

For now, do as @dave-mills suggests. You can also "override" the name of a trait method:

use Translatable {
    Translatable::handleRecordCreation as filamentHandleRecordCreation;
}

and then you can call the `filamentHandleRecordCreation` method in your code if and when needed.

lukas-frey avatar Sep 18 '24 16:09 lukas-frey

Thanks @lukas-frey for the idea of overriding the trait names. That's instantly tidied up my mess of a function!

dave-mills avatar Sep 19 '24 08:09 dave-mills