Home > Back-end >  Laravel 8 route not found using jetstream nav menu
Laravel 8 route not found using jetstream nav menu

Time:10-06

Fairly new to laravel 8 with some experience with laravel 7. I'm trying to add some additional pages to the default dashboard nav menu. however after adding the code, as I expect it to be, I get this error:

Symfony\Component\Routing\Exception\RouteNotFoundException
Route [accounts.index] not defined. (View: /home/some/path/resources/views/navigation-menu.blade.php) 

So here is what I have done code wise: in web.php i have the following route:

Route::middleware(['auth:sanctum', 'verified'])
    ->get('/accounts', [AccountController::class, 'index'])
   ->name('accounts');

I have a controller /app/Http/Controllers/AccountController.php as follows:

<?php

namespace App\Http\Controllers;

use App\Models\Account;
use Illuminate\Support\Facades\View;

class AccountController extends Controller
{
    //
    public function index() {
        $accounts = Account::all();

        return View::make('pages.accounts.index')->with('accounts', $accounts);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('pages.accounts.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }
}

I have a Model /app/Models/Account.php:

<?php

namespace App\Models;

use Eloquent;

class Account extends Eloquent
{

}

I also have blade templates for this page which i wont list for brevity as don't think this matters with the issue and at this point if I navigate to {url}/accounts the accounts index page is shown as intended.

However now I want to move this functionality into the jetstream dashboard so I can cut down on some development time and theme it similar to the default laravel concept.

The first thing I need is to add a new nav item next to Dashboard so I amended the default navigation-menu.blade.php file (/resources/views/navigation-menu.blade.php) by copying what it uses for dashboard and updating:

...
                <!-- Navigation Links -->
                <div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex">
                    <x-jet-nav-link href="{{ route('dashboard') }}" :active="request()->routeIs('dashboard')">
                        {{ __('Dashboard') }}
                    </x-jet-nav-link>
                </div>
                <div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex">
                    <x-jet-nav-link href="{{ route('accounts.index') }}" :active="request()->routeIs('accounts.index')">
                        {{ __('Accounts') }}
                    </x-jet-nav-link>
                </div>
            </div>
...

It is at this point when reloading the page throws the error. Anyone know what is causing this? I initially tried without using .index as this is the default anyway right?

I have also tried to follow a few tuts on doing this but I get the same error when i reload my page after editing the navigation-manu.blade.php file so I'm at a loss. I'm developing this on ubuntu 20.04

thanks

Craig

*** EDIT *** I have updated my route to be a resource for better use moving forward.

Route::middleware(['auth:sanctum', 'verified'])
    ->resource('/accounts', [AccountController::class, 'index'])
    ->name('accounts.index');

Tested this without the amend in navigation-menu.blade.php and all still worked added back the amends and same error.

CodePudding user response:

you need to call your route using it's name.

in your definition you are define the route with the name accounts while you are calling a route named by accounts.index

replace your route

Route::middleware(['auth:sanctum', 'verified'])
    ->get('/accounts', [AccountController::class, 'index'])
    ->name('accounts');

By

Route::middleware(['auth:sanctum', 'verified'])
    ->get('/accounts', [AccountController::class, 'index'])
    ->name('accounts.index');
           // note this

CodePudding user response:

Your route name is accounts and you called accounts.index, it will not work, you have two possibilities to fix them.

  1. replace :

    Route::middleware(['auth:sanctum', 'verified'])->get('/accounts', [AccountController::class, 'index'])->name('accounts')

by :

Route::middleware(['auth:sanctum', 'verified'])
        ->get('/accounts', [AccountController::class, 'index'])
        ->name('accounts.index')
  1. You can also use resources as method :

     Route::middleware(['auth:sanctum', 'verified'])->resource('/accounts', AccountController::class)->only(['index', 'create','store',update]);
    
  • Related