如何将vue.js文件中的信息像ID一样发送到控制器?Laravel-惯性 [英] How to send information from the vue.js file to the controller like an id? Laravel - Inertia-vue
问题描述
我正在尝试通过按钮打开页面,但是我需要发送按钮所在项目的ID.在上下文中,这是一个论坛论坛页面,在用户破折号中显示了他们所在的论坛可以发表评论,每个评论的底部都有一个按钮,我需要发送的是该论坛的ID,这样,当他们单击该按钮时,它们便会进入正确的论坛视图.
I'm trying to open a page from a button, but I need to send the id of the item where the button is in. For context this is a discussion forum page, in the users dash they are shown the forums they can comment on and there is a button on the bottom of each, what I need to send is that forums id, so that when they click on the button it takes them to the correct forum view.
我该怎么做?
我正在尝试不同的方式
<el-button type="primary" @click="submit(f.id)">
Answer
</el-button>
submit($id) {
var link = 'comments/' + $id;
this.$inertia.visit('comments/' + $id, {
$id,
});
},
<inertia-link class="el-button el-button--warning"
:href="'comments/' + f.id">
Answer
</inertia-link>
在我的路线上,web.php
In my routes web.php
Route::resource('comments', 'ReplyController');
Route::get('comments/{id}', 'ReplyController@index');
控制器中的索引
public function index($id)
{
$forum = DiscussionForum::where('id', $id)
->with('comment', 'user')->first();
$comment = Reply::with('discussionForum', 'user')
->where('discussion_forum_id', $forum->id)
->orderByDesc('updated_at')->get();
return Inertia::render('Forum/Comment.vue', [
'forum' => $forum,
'comments' => $comment
]);
}
这不起作用,重定向向我显示了主页内的空白窗口吗?如何正确发送论坛ID?
This is not working, the redirect shows me a blank window inside the main page? How can I send the forum id correctly?
推荐答案
最好的方法是在路线上添加一个名称,这样:
The best way to do this is to add a name to the route so:
Route::get('comments/{id}')->name('comments')->uses('ReplyController@index');
然后,您可以通过以下方式在惯性链接中传递您的 id
:
Then you can pass your id
in the inertia-link in this way:
<inertia-link class="el-button el-button--warning"
:href="route('comments', { f.id })">
Answer
</inertia-link>
这种方式可以与文档中提到的Ziggy一起使用. https://inertiajs.com/routing
This way can be used with Ziggy as mentioned in the docs. https://inertiajs.com/routing
这篇关于如何将vue.js文件中的信息像ID一样发送到控制器?Laravel-惯性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!