Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aj411's avatar
Level 1

Laravel project upgraded - issues with artisan optimize

I have recently shifted an old code base to Laravel 9. I am now having an issue when running php artisan optimize. This worked fine in the old version (Laravel 5.8) - It seems that the route name is the issue here but due to the prefix there is no issue in route list (why doesn't it just use the prefixed name as the key?). How do I go about fixing this?

API Route file

  Route::prefix('v3')
    ->middleware(['auth:api', 'throttle:rate_limit,1', 'token.usage', 'trace.request'])
    ->group(function () {

        // Achievements.
        Route::apiResource('achievements/definitions', AchievementDefinitionController::class);

        // Benefits.
        Route::apiResource('benefits/definitions', BenefitDefinitionController::class);


    });

Route listing

  GET|HEAD        api/v3/achievements/definitions ............................................................................................... definitions.index › AchievementDefinitionController@index
  POST            api/v3/achievements/definitions ............................................................................................... definitions.store › AchievementDefinitionController@store
  GET|HEAD        api/v3/achievements/definitions/{definition} .................................................................................... definitions.show › AchievementDefinitionController@show
  PUT|PATCH       api/v3/achievements/definitions/{definition} ................................................................................ definitions.update › AchievementDefinitionController@update
  DELETE          api/v3/achievements/definitions/{definition} .............................................................................. definitions.destroy › AchievementDefinitionController@destroy
  GET|HEAD        api/v3/benefits/definitions ....................................................................................................... definitions.index › BenefitDefinitionController@index
  POST            api/v3/benefits/definitions ....................................................................................................... definitions.store › BenefitDefinitionController@store
  GET|HEAD        api/v3/benefits/definitions/{definition} ............................................................................................ definitions.show › BenefitDefinitionController@show
  PUT|PATCH       api/v3/benefits/definitions/{definition} ........................................................................................ definitions.update › BenefitDefinitionController@update
  DELETE          api/v3/benefits/definitions/{definition} ...................................................................................... definitions.destroy › BenefitDefinitionController@destroy

Optimize error

  php artisan optimize

   INFO  Caching the framework bootstrap files.

  config ................................................................................................................................. 10ms DONE
  routes .................................................................................................................................. 6ms FAIL

   LogicException

  Unable to prepare route [api/v3/benefits/definitions] for serialization. Another route has already been assigned name [definitions.index].

  at vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php:247
    243▕             $route->name($this->generateRouteName());
    244▕
    245▕             $this->add($route);
    246▕         } elseif (! is_null($symfonyRoutes->get($name))) {
  ➜ 247▕             throw new LogicException("Unable to prepare route [{$route->uri}] for serialization. Another route has already been assigned name [{$name}].");
    248▕         }
    249▕
    250▕         $symfonyRoutes->add($route->getName(), $route->toSymfonyRoute());
    251▕

      +35 vendor frames
  36  artisan:35
0 likes
5 replies
AddWebContribution's avatar

@aj411 you definitions.index is using in two different route try to use definitions_another.index for other and then optimize it it will work

aj411's avatar
Level 1

I understand that definitions.index is already in use - my question is why?

Why isn't it using achievements/definitions as the name, do all route names need to be unique in laravel 9?

How can I define a different name without changing the route url?

Snapey's avatar

@aj411 the name and the route can be anything you like, but if you don't specify a name then the resource name is used , ie 'definitions' in both cases

just add a route name

aj411's avatar
Level 1

Thanks @snapey how do I add a name to a apiResource route if not via the name parameter?

Please or to participate in this conversation.