简体   繁体   English

Laravel 带 id 的惯性链接

[英]Laravel inertia link with id

Hello I am beginner in using inertia and laravel, I want to pass an id from the web.php routes of my laravel and i receive error.你好我是使用惯性和 laravel 的初学者,我想从我的 laravel 的web.php路由传递一个 id,但我收到错误。

Here is my web.php:这是我的 web.php:

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

now my route/href is like this:现在我的路线/href 是这样的:

<inertia-link class="btn btn-primary btn-sm" href="/member/{{ member.id }}/show">

can anyone help me fix the href="/member/{{ member.id }}/show"谁能帮我修复href="/member/{{ member.id }}/show"

you can use您可以使用

<InertiaLink :href="route('member.show', { id: member.id })">
 show
</InertiaLink>

for more info here更多信息在这里

If you want to link to a new vue component , send an id and show the id in the url , you can do this:如果你想链接到一个新的vue component ,发送一个id并在url中显示id ,你可以这样做:

Vue side : Vue side

<template>
  <button @click="submit">Open page with id in url</button>
</template>


<script setup>
import {Inertia} from "@inertiajs/inertia";

const submit = () => {
    const id = 1
    Inertia.post('/yourUrl/' + id, {id: id}) //{id: id} key-value-pair of id
}
</script>

Laravel side (web.php) : Laravel side (web.php)

Route::post('/yourUrl/{id}', function (Request $request) {
    return inertia('yourVueComponent', ['id' => $request->id]);
});

I can't comment yet so I'll post this as an answer.我还不能发表评论,所以我会把这个作为答案发布。

@mohamed-elshazly is nearly correct but got one minor detail wrong: @mohamed-elshazly 几乎是正确的,但有一个小细节错误:

<InertiaLink :href="route('member.show', { member: member.id })">
 show
</InertiaLink>

It's like that, the parameter in the route is called {member} so you pass it an object with a member parameter: { member: yourVariableForMember }就像那样,路由中的参数称为 {member} 所以你向它传递一个带有成员参数的对象:{ member: yourVariableForMember }

Whatever the parameter in your route is called, you use that in place of member.无论您的路由中的参数是什么,您都可以使用它来代替成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM