如何在资源路由URL中显示ID?

发布于 2025-01-28 11:26:52 字数 1959 浏览 4 评论 0 原文

更新:

前端中的代码行是罪魁祸首:

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

Update:

This line of code in the frontend was the culprit:

<inertia-link v-if="options.edit" :href="'/admin/gallery/edit/1'">

I had to change it to:

<inertia-link v-if="options.edit" :href="'/admin/gallery/1/edit'">

to make it comply with the laravel resource format for edit, provided by @Babak.

Original Post:

How would I transform this route in web.php:

Route::get('/admin/gallery/edit/{id}', function ($id) {
    $data = Gallery::find($id);
    return inertia('backend/cms-gallery-edit', ['data' => $data]);
});

to a resource route with its resource controller function:

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

Edit:

I've tried both approaches of @Babak's answer, which work for index and create routes but the edit route still throws a 404. It is the only route encompassing an id.

web.php:

Route::resource('/admin/gallery', GalleryController::class)->only('index', 'create', 'edit');

GalleryController.php:

public function edit($gallery)
{
    $data = Gallery::find($gallery);
    return inertia('backend/cms-gallery-edit', ['data' => $data]);
}

Inertia passes the id from the frontend via href:

<inertia-link v-if="options.edit" :href="'/admin/gallery/edit/1'">

Browser shows:

GET http://127.0.0.1:8000/admin/gallery/edit/1 404 (Not Found)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花随流水 2025-02-04 11:26:52

Laravel资源路由方法有一个固定的结构,您可以看到完整列表在这里。对于编辑页面,它将生成'/admin/Gallery/{Gallery}/edit'之类的内容,

以下方式写入:

Route::resource('/admin/gallery', GalleryController::class)->only('edit');

可以按照 在您的控制器中,资源的名称必须与函数的参数相同。

public function edit($gallery)
{
    $data = Gallery::find($gallery);
    // assign id to end of route
    return inertia('backend/cms-gallery-edit', ['data' => $data]);
}

或者,您可以使用参数方法自定义它。请参阅在这里

Route::resource('/admin/gallery', GalleryController::class)->only('edit')->parameters([
    'gallery' => 'id'
]);

和您的控制器

public function edit($id)
{
    $data = Gallery::find($id);
    // assign id to end of route
    return inertia('backend/cms-gallery-edit', ['data' => $data]);
}

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:

Route::resource('/admin/gallery', GalleryController::class)->only('edit');

And in your controller, name of the resource must be the same as your function's parameter.

public function edit($gallery)
{
    $data = Gallery::find($gallery);
    // assign id to end of route
    return inertia('backend/cms-gallery-edit', ['data' => $data]);
}

Or, you can customize it using parameter method. Refer to here

Route::resource('/admin/gallery', GalleryController::class)->only('edit')->parameters([
    'gallery' => 'id'
]);

And your controller

public function edit($id)
{
    $data = Gallery::find($id);
    // assign id to end of route
    return inertia('backend/cms-gallery-edit', ['data' => $data]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文