How to send information from the vue.js file to the controller like an id? Laravel - Inertia-vue

冷暖自知 提交于 2021-01-29 09:59:54

问题


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.

How can I do this?

I'm trying different ways

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

In my routes web.php

Route::resource('comments', 'ReplyController');
Route::get('comments/{id}', 'ReplyController@index');

The index in the controller

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

This is not working, the redirect shows me a blank window inside the main page? How can I send the forum id correctly?


回答1:


The best way to do this is to add a name to the route so:

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

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>

This way can be used with Ziggy as mentioned in the docs. https://inertiajs.com/routing




回答2:


Use the emit function here: https://vuejs.org/v2/guide/components-custom-events.html

Something like this:

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


来源:https://stackoverflow.com/questions/58840998/how-to-send-information-from-the-vue-js-file-to-the-controller-like-an-id-larav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!