routeslaravel-8ziggy

Laravel route naming bug: not getting resource route naming as supposed to


I am facing an issue with Laravel / React.js app

I have this web.route configuration:

Route::middleware(['auth', 'verified'])->group(function () {
    Route::redirect('/admin', '/admin/dashboard');
    Route::get('/admin/dashboard', [DashboardController::class, 'index'])->name('adminDashboard');
    Route::resource('/admin/properties', PropertyController::class);
    Route::resource('/admin/property-types/', PropertyTypeController::class);
    Route::resource('/admin/property-meta', PropertyMetaController::class);
    Route::resource('/admin/transaction-types', TransactionTypeController::class);
    Route::resource('/admin/amenities', AmenityController::class);
    Route::resource('/admin/posts', PostController::class);
    Route::resource('/admin/post-categories', PostCategoryController::class);
    Route::resource('/admin/bookings', BookingController::class);
    Route::resource('/admin/users', UserController::class);
    Route::resource('/admin/roles', RoleController::class);
    Route::resource('/admin/permissions', PermissionController::class);
});

The route:list output is:

PUT|PATCH       admin/property-meta/{property_metum} ......................... property-meta.update › Admin\PropertyMetaController@update  
  DELETE          admin/property-meta/{property_metum} ....................... property-meta.destroy › Admin\PropertyMetaController@destroy  
  GET|HEAD        admin/property-meta/{property_metum}/edit ........................ property-meta.edit › Admin\PropertyMetaController@edit  
  GET|HEAD        admin/property-types ......................................................... index › Admin\PropertyTypeController@index  
  POST            admin/property-types ......................................................... store › Admin\PropertyTypeController@store  
  GET|HEAD        admin/property-types/create ................................................ create › Admin\PropertyTypeController@create  
  GET|HEAD        admin/property-types/{} ........................................................ show › Admin\PropertyTypeController@show  
  PUT|PATCH       admin/property-types/{} .................................................... update › Admin\PropertyTypeController@update  
  DELETE          admin/property-types/{} .................................................. destroy › Admin\PropertyTypeController@destroy  
  GET|HEAD        admin/property-types/{}/edit ................................................... edit › Admin\PropertyTypeController@edit  
  GET|HEAD        admin/roles .................................................................... roles.index › Admin\RoleController@index  
  POST            admin/roles .................................................................... roles.store › Admin\RoleController@store  
  GET|HEAD        admin/roles/create ........................................................... roles.create › Admin\RoleController@create 

in the output you can see that the property-types/create route name is 'create' while first it was 'property-types.create'

I tried to fix it by redefining the route as:

Route::resource('/admin/property-types/', PropertyTypeController::class)->names('property-types');

It worked however I am getting strange url generation for the edit route. It giving this URL:

route('property-types.edit', propertyType.id)

http://realestate-laravel/admin/property-types/%7B%7D/edit?6=

I tried to add fake route with name index to force propertyTypes to not use this route, but I couldn't compile the route cache.

Route::middleware(['auth', 'verified'])->group(function () {
    Route::redirect('/admin', '/admin/dashboard');
    Route::get('/admin/dashboard', [DashboardController::class, 'index'])->name('adminDashboard');
    Route::get('/admin/x', [DashboardController::class, 'index'])->name('index'); //here! forcing this route to take the 'index' route
    Route::resource('/admin/properties', PropertyController::class);
    Route::resource('/admin/property-types/', PropertyTypeController::class); //this rule has no names defined. should take property-types.index .create .store ...
    Route::resource('/admin/property-meta', PropertyMetaController::class);
    Route::resource('/admin/transaction-types', TransactionTypeController::class);
    Route::resource('/admin/amenities', AmenityController::class);
    Route::resource('/admin/posts', PostController::class);
    Route::resource('/admin/post-categories', PostCategoryController::class);
    Route::resource('/admin/bookings', BookingController::class);
    Route::resource('/admin/users', UserController::class);
    Route::resource('/admin/roles', RoleController::class);
    Route::resource('/admin/permissions', PermissionController::class);
});

When running route:cache I am getting this:

  Unable to prepare route [admin/property-types] for serialization. Another route has already been assigned name [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▕

  1   vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php:208
      Illuminate\Routing\AbstractRouteCollection::addToSymfonyRoutesCollection(Object(Symfony\Component\Routing\RouteCollection), Object(Illuminate\Routing\Route))

  2   vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php:247
      Illuminate\Routing\AbstractRouteCollection::toSymfonyRouteCollection()

Solution

  • It's fixed now!

    The issue was there was a backslash at the end of the route name.

    Route::resource('/admin/property-types', PropertyTypeController::class);
    

    Fix

    Route::resource('/admin/property-types/', PropertyTypeController::class);