Nancy
Nancy

Reputation: 1051

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

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?

Upvotes: 1

Views: 4089

Answers (2)

Fabi D
Fabi D

Reputation: 89

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

Something like this:

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

Upvotes: 0

Luca Rossi
Luca Rossi

Reputation: 776

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

Upvotes: 3

Related Questions