Search code examples
phphtmllaravellaravel-8laravel-9

LARAVEL 9 Route [posts.all] not defined error


For some reason Laravel says the route 'posts.all' is not defined. here's my code:

html:

<li>
   <a href="{{route('posts.all')}}" class=" waves-effect">
      <i class="ri-calendar-2-line"></i>
         <span>All Posts</span>
   </a>
</li>

Laravel Controller group with middleware:

Route::middleware(['auth'])->name('dashboard')->group(function(){
    Route::controller(\App\Http\Controllers\PostController::class)->group(function(){
        Route::get('/dashboard', 'Dashboard')->name('dashboard');
        Route::get('/posts/all', 'AllPosts')->name('posts.all');
    });
});;

however, when i refactor the code to this, it works, but i want to keep in in the PostController

Route::get('/posts/all', [\App\Http\Controllers\PostController::class, 'AllPosts'])->name('posts.all');

Solution

  • you are grouping everything with the dashboard name.

    ->name('dashboard')->group(function(){});
    

    So your route name ends up being dashboardposts.all, this can be debugged with php artisan route:list.

    Assuming you want this structure, i would add a dash between dashboard and posts name, by calling the first name group.

    ->name('dashboard.')
    

    Then access it like so.

    route('dashboard.posts.all')