如何将vue.js文件中的信息像ID一样发送到控制器? Laravel-惯性

时间:2019-11-13 16:01:00

标签: laravel inertia

我正在尝试通过按钮打开页面,但是我需要发送按钮所在项目的ID。对于上下文,这是一个论坛论坛页面,在用户破折号中显示了他们所在的论坛可以发表评论,并且每个评论的底部都有一个按钮,我需要发送的是该论坛的ID,这样,当他们单击该按钮时,它们便会进入正确的论坛视图。

我该怎么做?

我正在尝试不同的方式

<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

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?

2 个答案:

答案 0 :(得分:1)

执行此操作的最佳方法是为路线添加名称,这样:

Route::get('comments/{id}')->name('comments')->uses('ReplyController@index');

然后,您可以通过以下方式在惯性链接中传递id

<inertia-link class="el-button el-button--warning"
    :href="route('comments', { f.id })">
        Answer
</inertia-link>

这种方式可以与文档中提到的Ziggy一起使用。 https://inertiajs.com/routing

答案 1 :(得分:0)

在此处使用发射功能:https://vuejs.org/v2/guide/components-custom-events.html

类似这样的东西:

yourMethod() {
  this.$emit(
    "someName",
    this.id
  );
}