admin管理员组文章数量:1287511
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 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 ?
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/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 ?
Share Improve this question edited Feb 23 at 18:25 keyon8060 asked Feb 23 at 17:19 keyon8060keyon8060 93 bronze badges3 Answers
Reset to default 1For 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);
}
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');
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');
});
版权声明:本文标题:laravel 11 : resource route doesn't work the same way when it's detailed in some route lines - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741310057a2371598.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论