filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
19.21k stars 2.96k forks source link

Creating a resource for a model named Record, breaks php artisan command #9155

Closed Tray2 closed 1 year ago

Tray2 commented 1 year ago

Package

filament/filament

Package Version

v3.0.´-stable

Laravel Version

v10.28

Livewire Version

v3.0

PHP Version

PHP 8.2.9

Problem description

The command php artisanin general stopped working, it throws an ugly error message saying this.

(Had to put a link to a repo to be able to submit this, the one I put does not link to any gist or repo that describes this error)

php artisan

   Symfony\Component\ErrorHandler\Error\FatalError

  Cannot declare class App\Filament\Resources\RecordResource\Pages\CreateRecord because the name is already in use

  at app/Filament/Resources/RecordResource/Pages/CreateRecord.php:9
      5▕ use App\Filament\Resources\RecordResource;
      6▕ use Filament\Actions;
      7▕ use Filament\Resources\Pages\CreateRecord;
      8▕
  ➜   9▕ class CreateRecord extends CreateRecord
     10▕ {
     11▕     protected static string $resource = RecordResource::class;
     12▕ }
     13▕

   Whoops\Exception\ErrorException

  Cannot declare class App\Filament\Resources\RecordResource\Pages\CreateRecord because the name is already in use

  at app/Filament/Resources/RecordResource/Pages/CreateRecord.php:9
      5▕ use App\Filament\Resources\RecordResource;
      6▕ use Filament\Actions;
      7▕ use Filament\Resources\Pages\CreateRecord;
      8▕
  ➜   9▕ class CreateRecord extends CreateRecord
     10▕ {
     11▕     protected static string $resource = RecordResource::class;
     12▕ }
     13▕

      +1 vendor frames

  2   [internal]:0
      Whoops\Run::handleShutdown()

Expected behavior

See a list of available artisan commands.

php artisan
Laravel Framework 10.28.0

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display help for the given command. When no command is given display help for the list command
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi|--no-ansi  Force (or disable --no-ansi) ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  about                                 Display basic information about your application
  clear-compiled                        Remove the compiled class file
  completion                            Dump the shell completion script
  db                                    Start a new database CLI session
  docs                                  Access the Laravel documentation
  down                                  Put the application into maintenance / demo mode
  env                                   Display the current framework environment
  help                                  Display help for a command
  inspire                               Display an inspiring quote
  list                                  List commands
  migrate                               Run the database migrations
  optimize                              Cache the framework bootstrap files
  serve                                 Serve the application on the PHP development server
  test                                  Run the application tests
  tinker                                Interact with your application
  up                                    Bring the application out of maintenance mode
 auth
  auth:clear-resets                     Flush expired password reset tokens
 breeze
  breeze:install                        Install the Breeze controllers and resources
 cache
  cache:clear                           Flush the application cache
  cache:forget                          Remove an item from the cache
  cache:prune-stale-tags                Prune stale cache tags from the cache (Redis only)
  cache:table                           Create a migration for the cache database table
 channel
  channel:list                          List all registered private broadcast channels
 config
  config:cache                          Create a cache file for faster configuration loading
  config:clear                          Remove the configuration cache file
  config:show                           Display all of the values for a given configuration file
 db
  db:monitor                            Monitor the number of connections on the specified database
  db:seed                               Seed the database with records
  db:show                               Display information about the given database
  db:table                              Display information about the given database table
  db:wipe                               Drop all tables, views, and types
 env
  env:decrypt                           Decrypt an environment file
  env:encrypt                           Encrypt an environment file
 event
  event:cache                           Discover and cache the application's events and listeners
  event:clear                           Clear all cached events and listeners
  event:generate                        Generate the missing events and listeners based on registration
  event:list                            List the application's events and listeners
 filament
  filament:assets                       Set up Filament assets.
  filament:check-translations           Check for missing and removed translations
  filament:install                      Install Filament.
  filament:upgrade                      Upgrade Filament to the latest version
 icons
  icons:cache                           Discover icon sets and generate a manifest file
  icons:clear                           Remove the blade icons manifest file
 key
  key:generate                          Set the application key
 lang
  lang:publish                          Publish all language files that are available for customization
 livewire
  livewire:attribute                    Create a new Livewire attribute class
  livewire:configure-s3-upload-cleanup  Configure temporary file upload s3 directory to automatically cleanup files older than 24hrs.
  livewire:copy                         Copy a Livewire component
  livewire:delete                       Delete a Livewire component
  livewire:form                         Create a new Livewire form class
  livewire:layout                       Create a new app layout file
  livewire:make                         Create a new Livewire component
  livewire:move                         Move a Livewire component
  livewire:publish                      Publish Livewire configuration
  livewire:stubs                        Publish Livewire stubs
  livewire:upgrade                      Interactive upgrade helper to migrate from v2 to v3
 make
  make:cast                             Create a new custom Eloquent cast class
  make:channel                          Create a new channel class
  make:command                          Create a new Artisan command
  make:component                        Create a new view component class
  make:controller                       Create a new controller class
  make:event                            Create a new event class
  make:exception                        Create a new custom exception class
  make:factory                          Create a new model factory
  make:filament-page                    Create a new Filament page class and view
  make:filament-panel                   Create a new Filament panel
  make:filament-relation-manager        Create a new Filament relation manager class for a resource
  make:filament-resource                Create a new Filament resource class and default page classes
  make:filament-theme                   Create a new Filament panel theme
  make:filament-user                    Create a new Filament user
  make:filament-widget                  Create a new Filament widget class
  make:form-field                       Create a new form field class and view
  make:form-layout                      Create a new form layout component class and view
  make:infolist-entry                   Create a new infolist entry class and view
  make:infolist-layout                  Create a new infolist layout component class and view
  make:job                              Create a new job class
  make:listener                         Create a new event listener class
  make:livewire                         Create a new Livewire component
  make:livewire-form                    Create a new Livewire component containing a Filament form
  make:livewire-table                   Create a new Livewire component containing a Filament table
  make:mail                             Create a new email class
  make:middleware                       Create a new middleware class
  make:migration                        Create a new migration file
  make:model                            Create a new Eloquent model class
  make:notification                     Create a new notification class
  make:observer                         Create a new observer class
  make:policy                           Create a new policy class
  make:provider                         Create a new service provider class
  make:request                          Create a new form request class
  make:resource                         Create a new resource
  make:rule                             Create a new validation rule
  make:scope                            Create a new scope class
  make:seeder                           Create a new seeder class
  make:table-column                     Create a new table column class and cell view
  make:test                             Create a new test class
  make:view                             Create a new view
 migrate
  migrate:fresh                         Drop all tables and re-run all migrations
  migrate:install                       Create the migration repository
  migrate:refresh                       Reset and re-run all migrations
  migrate:reset                         Rollback all database migrations
  migrate:rollback                      Rollback the last database migration
  migrate:status                        Show the status of each migration
 model
  model:prune                           Prune models that are no longer needed
  model:show                            Show information about an Eloquent model
 notifications
  notifications:table                   Create a migration for the notifications table
 optimize
  optimize:clear                        Remove the cached bootstrap files
 package
  package:discover                      Rebuild the cached package manifest
 pest
  pest:dataset                          Create a new dataset file
  pest:test                             Create a new test file
 queue
  queue:batches-table                   Create a migration for the batches database table
  queue:clear                           Delete all of the jobs from the specified queue
  queue:failed                          List all of the failed queue jobs
  queue:failed-table                    Create a migration for the failed queue jobs database table
  queue:flush                           Flush all of the failed queue jobs
  queue:forget                          Delete a failed queue job
  queue:listen                          Listen to a given queue
  queue:monitor                         Monitor the size of the specified queues
  queue:prune-batches                   Prune stale entries from the batches database
  queue:prune-failed                    Prune stale entries from the failed jobs table
  queue:restart                         Restart queue worker daemons after their current job
  queue:retry                           Retry a failed queue job
  queue:retry-batch                     Retry the failed jobs for a batch
  queue:table                           Create a migration for the queue jobs database table
  queue:work                            Start processing jobs on the queue as a daemon
 route
  route:cache                           Create a route cache file for faster route registration
  route:clear                           Remove the route cache file
  route:list                            List all registered routes
 sail
  sail:add                              Add a service to an existing Sail installation
  sail:install                          Install Laravel Sail's default Docker Compose file
  sail:publish                          Publish the Laravel Sail Docker files
 sanctum
  sanctum:prune-expired                 Prune tokens expired for more than specified number of hours
 schedule
  schedule:clear-cache                  Delete the cached mutex files created by scheduler
  schedule:interrupt                    Interrupt the current schedule run
  schedule:list                         List all scheduled tasks
  schedule:run                          Run the scheduled commands
  schedule:test                         Run a scheduled command
  schedule:work                         Start the schedule worker
 schema
  schema:dump                           Dump the given database schema
 session
  session:table                         Create a migration for the session database table
 storage
  storage:link                          Create the symbolic links configured for the application
 stub
  stub:publish                          Publish all stubs that are available for customization
 vendor
  vendor:publish                        Publish any publishable assets from vendor packages
 view
  view:cache                            Compile all of the application's Blade templates
  view:clear                            Clear all compiled view files

Steps to reproduce

  1. Create the model and migration for the record model php artisan make:model Record -m
  2. Create the filament resource for the Record model php artisan make:filament-resource Record
  3. Run any artisan command php artisan
  4. See it blow up.

Reproduction repository

https://github.com/Tray2

Relevant log output

No response

zepfietje commented 1 year ago

Thanks for reporting this, @Tray2. The error makes sense, since the class name collides with the one it extends.

Since this is very low priority for us to fix at the moment, could you attempt a PR, @Tray2? You could probably check for the model name and import the extended class as BaseCreateRecord for example.

sinnbeck commented 1 year ago

For now you can just import as an alias


use Filament\Resources\Pages\CreateRecord as FilamentCreateRecord;

class CreateRecord extends FilamentCreateRecord
Tray2 commented 1 year ago

@zepfietje yes it makes total sense. I will either find a synonym or use an alias in the import for now. Not sure how to work around it when a model has a naming clash like this in a good way though.

Low priority indeed, and good as a reference if someone else runs into a similar issue.

zepfietje commented 1 year ago

Not sure how to work around it when a model has a naming clash like this in a good way though.

What are you unsure about? 🙂

sinnbeck commented 1 year ago

Maybe something like this (just an example of the idea. Would need cleanup)


            $this->copyStubToApp('ResourcePage', $createResourcePagePath, [
                'baseResourcePage' => $createResourcePageClass === 'CreatePage' ? 'Filament\\Resources\\Pages\\CreateRecord as BaseCreateRecord' : 'Filament\\Resources\\Pages\\CreateRecord',
                'baseResourcePageClass' => $createResourcePageClass === 'CreatePage' ? 'BaseCreateRecord' : 'CreateRecord',
                'namespace' => "{$namespace}\\{$resourceClass}\\Pages",
                'resource' => "{$namespace}\\{$resourceClass}",
                'resourceClass' => $resourceClass,
                'resourcePageClass' => $createResourcePageClass,
            ]);
Tray2 commented 1 year ago

Not sure how to work around it when a model has a naming clash like this in a good way though.

What are you unsure about? 🙂

How it should be fone in a pretty, effective and standardized way. However it seems to be solved already.