Home > Mobile >  Laravel Route [blood-camp] not defined exception
Laravel Route [blood-camp] not defined exception

Time:11-17

I'm getting this error even though the route is defined

Route [blood-camp] not defined. (View: C:\wamp64\www\blood-donation\resources\views\layouts\app.blade.php)

web.php file

`Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home.root');

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Route::get('/user', [App\Http\Controllers\UserController::class, 'index'])->name('user');



Route::resource('blood-camp', BloodCampController::class);

Route::resource('donor', DonorController::class);

Route::resource('camp-schedule', CampScheduleController::class);`

app.blade.php

 <li >
 <a  href="{{ route('blood-camp') }}" role="button" >
                                {{ __('Camps') }}
</a>
</li>

controller class

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class BloodCampController extends Controller
{
  public function __construct()
    {
        $this->middleware('auth');
    }

public function index()
    {        
        return view('blood_camp.index', ['camps' =>        DB::table('blood_camps')->orderBy('name', 'ASC')->paginate(10)]);
    }

//all the other resources are there...

}

route:list

There is no duplicate route

enter image description here

I have tried all the suggested ways including changing path names, cache clearing, private window, restarting server, but I'm still getting this error and no way to go further developments.

It first occurred when I adding new route called camp, I changed the name to blood-camp but no luck, now it throw exceptions for the other routes as well. Can someone please explain me what I'm doing wrong?

CodePudding user response:

As the result of your route:list shows, you dont have a route with the alias blood-camp.

What you have as alias is blood-camp.index, blood-camp.store....

To generate the link of the ressources listing use:

By path/url

{{ url('blood-camp') }}

Or by alias

{{ route('blood-camp.index') }}

CodePudding user response:

I have sorted this out

The issue was Route::resource() generates named routes for the resources such as `blood-camp.index, blood-camp.store'

So I explicitly added that named route as follows;

Route::resource('blood-camp', BloodCampController::class,[
    'names' => [
        'index' => 'blood-camp',        
    ]
]);

and run optimize and now it is working with route('blood-camp')

  • Related