keyon8060
keyon8060

Reputation: 9

laravel 11 : resource route doesn't work the same way when it's detailed in some route lines

laravel 11 here

in web.php

Route::resource('event', EventController::class);

works ok

when i replace this line by

Route::get('/event', [EventController::class, 'index'])->name('event.index');

Route::get('/event/{id}', [EventController::class, 'show'])->name('event.show');

show doesn't work anymore and send this error :

Attempt to read property "description" on null

because the object is not passed to the show method. i saw this with a dd($event); $event is the object that should be passed to the show method.

i need to detail each route instead of simply using Route::resource because index and show should be public and the rest should be authenticated.

any hint ? thank you

the Route::resource should work the same way the two Route::get lines work

EDIT it seems like if i write

Route::get('event/{event}', [EventController::class, 'show'])->name('event.show');

it works but i don't get why. (i put event instead of id)

i did the same as in this video https://www.youtube.com/watch?v=eUNWzJUvkCA but it doesn't work the same when i do it

EDIT my routes look like this now :

//guest
Route::get('event', [EventController::class, 'index'])->name('event.index');
Route::get('event/{event}', [EventController::class, 'show'])->name('event.show');

// authenticated        
Route::middleware(['auth'])->group(function () {
    Route::get('/event/create', [EventController::class, 'create'])->name('event.create');
    Route::post('/event', [EventController::class, 'store'])->name('event.store');
    Route::get('/event/{event}/edit', [EventController::class, 'edit'])->name('event.edit');
    Route::put('/event/{event}', [EventController::class, 'update'])->name('event.update');
    Route::delete('/event/{event}', [EventController::class, 'destroy'])->name('event.destroy');
});

but localhost:8000/event/create returns a 404. how strange. any hint ?

Upvotes: 0

Views: 59

Answers (3)

kris gjika
kris gjika

Reputation: 1476

route resources give a lot of flexibility, so you don't need to drop them just to have 2 middleware applied.

As an example:

Route::resource('event', EventController::class)->only('index', 'show');
Route::resource('event', EventController::class)->except('index', 'show')->middleware('auth');

is the same as your 9 line route declarations

Route::get('event', [EventController::class, 'index'])->name('event.index');
Route::get('event/{event}', [EventController::class, 'show'])->name('event.show');

Route::middleware(['auth'])->group(function () {
    Route::get('/event/create', [EventController::class, 'create'])->name('event.create');
    Route::post('/event', [EventController::class, 'store'])->name('event.store');
    Route::get('/event/{event}/edit', [EventController::class, 'edit'])->name('event.edit');
    Route::put('/event/{event}', [EventController::class, 'update'])->name('event.update');
    Route::delete('/event/{event}', [EventController::class, 'destroy'])->name('event.destroy');
});

Upvotes: 0

Francisco Rojas
Francisco Rojas

Reputation: 21

For it to work you have to pass the event (your model) as a parameter to the function and from the view pass the full value. Check your controller. Here is an example.

public function show (Event $event)
{
    return view('event.show', $event);
}

Upvotes: 1

keyon8060
keyon8060

Reputation: 9

I discovered that order is important in declaring laravel routes and that some can be greedy.

the first example doesn't work because the first line takes precedence over the second one

Route::get('/event/{event}', [EventController::class, 'show'])->name('event.show');
Route::get('/event/create', [EventController::class, 'create'])->name('event.create');

The right way to write routes in order is below

Route::get('/event/create', [EventController::class, 'create'])->name('event.create');
Route::get('/event/{event}', [EventController::class, 'show'])->name('event.show');

Upvotes: 0

Related Questions