如何在资源路由URL中显示ID?
更新:
前端中的代码行是罪魁祸首:
<inertia-link v-if="options.edit" :href="'/admin/gallery/edit/1'">
我必须将其更改为:
<inertia-link v-if="options.edit" :href="'/admin/gallery/1/edit'">
要使它符合 laravel资源格式对于编辑
,由@babak提供。
原始帖子:
我将如何在 Web.php
中转换此路线:
Route::get('/admin/gallery/edit/{id}', function ($id) {
$data = Gallery::find($id);
return inertia('backend/cms-gallery-edit', ['data' => $data]);
});
带有其资源控制器功能的资源路线:
Route::resource('/admin/gallery', GalleryController::class);
GalleryController.php
:
public function edit($id)
{
$data = Gallery::find($id);
// assign id to end of route
return inertia('backend/cms-gallery-edit', ['data' => $data]);
}
编辑:
我尝试了 @babak答案的两种方法,哪种方法适用于 index
和 create> create> create < /code>路由,但是
编辑
路由仍然抛出 404
。这是唯一包含 ID
的路线。
web.php
:
Route::resource('/admin/gallery', GalleryController::class)->only('index', 'create', 'edit');
allerycontroller.php
:
public function edit($gallery)
{
$data = Gallery::find($gallery);
return inertia('backend/cms-gallery-edit', ['data' => $data]);
}
intertia通过前端传递 id
通过HREF:
<inertia-link v-if="options.edit" :href="'/admin/gallery/edit/1'">
浏览器显示:
GET http://127.0.0.1:8000/admin/gallery/edit/1 404 (Not Found)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Laravel资源路由方法有一个固定的结构,您可以看到完整列表在这里。对于编辑页面,它将生成
'/admin/Gallery/{Gallery}/edit'
之类的内容,以下方式写入:
您
可以按照 在您的控制器中,资源的名称必须与函数的参数相同。
或者,您可以使用
参数
方法自定义它。请参阅在这里和您的控制器
There is a fixed structure for laravel resource route method, you can see full list here. For edit page, it will generate something like
'/admin/gallery/{gallery}/edit'
You can write it like below:
In your
web.php
file:And in your controller, name of the resource must be the same as your function's parameter.
Or, you can customize it using
parameter
method. Refer to hereAnd your controller