Home > database >  Laravel redirecting to the wrong route
Laravel redirecting to the wrong route

Time:11-03

web.php

 Route::get('/gigs/{id}', [GigsController::class, 'info'])->name('clientside.gigs_info');

Route::get('/gigs/create', [GigsController::class, 'create'])->name('clientside.gigs.create');

Controller

public function create()
{
    $categories = Category::select('id', 'name')->get();

    return view('clientview.gigs.create', compact('categories'));
}


public function info($id)
{


    $gig = Gigs::join('users', 'users.id', '=', 'gigs.created_by')
        ->join('categories', 'categories.id', '=', 'gigs.category_id')
        ->select('gigs.*', 'categories.name as category_name', 'users.name as user_name', 'users.surname')
        ->where('gigs.id', '=', $id)
        ->orderBy('name', 'ASC')
        ->firstOrFail();

    return view('clientview.gigs.info', compact('gig'));
}

When I try to click this:

<a class="dropdown-item" href="{{ route('clientside.gigs.create') }}">Create Gigs</a>

When I click this I can observe from DebugBar that it directs to route ('clientside.gigs_info') I think "/create" thinks it is an /{ID} but however, I direct to a different route

CodePudding user response:

Answer by Michael Mano, Make sure you write on web.php static routes before dynamic.

CodePudding user response:

Just write create route before info route because it is dynamic route (That accept parameter) so always write dynamic route after the static route.

CodePudding user response:

You actually created a dynamic route gigs/{id} so anything that comes after gigs will be called as a parameter of gigs. So to fix this change the order in your web.php like below. So it will search for static route first and then go for dynamic route.

Route::get('/gigs/create', [GigsController::class, 'create'])->name('clientside.gigs.create'); Route::get('/gigs/{id}', [GigsController::class, 'info'])->name('clientside.gigs_info');

  • Related