Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel route resolving to a different method

I’m trying to setup a very basic laravel CRUD application and I’m getting stuck in setting up the pages for each action.

When I visit the route case/create, it opens the page for show instead.

routes/web.php

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

use App\Http\Controllers\HospitalCase as HospitalCase;

Route::controller(HospitalCase::class)->group(function(){
    Route::get('/cases','index')->middleware('auth')->name('cases');
    Route::get('/case/{id}','show')->middleware('auth');
    Route::get('/case/create','create')->middleware('auth');
    Route::post('/case/create','store')->middleware('auth');
    Route::get('/case/edit/{$id}','edit')->middleware('auth');
    Route::post('/case/edit/{$id}','update')->middleware('auth');
    Route::get('/case/delete/{$id}','destroy')->middleware('auth');
});

HospitalCase.php controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HospitalCase extends Controller
{
    function index()
    {
        echo 'index';
    }

    function create()
    {
        echo 'create';
    }

    function show($id)
    {
        echo 'show';
    }

    function store()
    {
        // validation rules
    }

    function edit($id)
    {
        return view('case/edit');
    }

    function update($id)
    {
        
    }

    function destroy($id)
    {
        
    }
}

This is what I see on the browser:

I have been trying to figure this out for hours and can’t think of what I’m doing wrong.

PS: The auth middleware is using laravel breeze (unmodified)

>Solution :

The reason it’s showing the show route is because you defined

Route::get('/case/{id}','show')->middleware('auth');

before it, therefore, it’s matching case/create as show('create')

Try defining the route afterwards.

Route::get('/case/create','create')->middleware('auth');
Route::post('/case/create','store')->middleware('auth');
Route::get('/case/{id}','show')->middleware('auth');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading