-1

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 ?

3 Answers 3

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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');

1 Comment

In the code you shared the top declarations are not conficting i.e. event and event/{id} should both work regardless of the order you declare them in. This doesn't seem relevant to that problem. What's most likely relevant is the fact that if you use route model binding it is important to name the route parameter the same as the route handler variable name e.g. if you have event/{id} your route handler needs to be function show(Event $id) and won't work with another name.
0

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');
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.