1

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
  • What does the f variable output? Have a look in your developer tools in the networking tab to see if you get any 500 errors, or the like. Is APP_DEBUG set to true? Commented Nov 19, 2019 at 14:59

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

This answer only works if you're using Ziggy by TightenCo(or if you have sort of other route method wired up somewhere).
Ziggy is suggested to use when working with Inerita. It's said also in the offical documentation
Sure, I didn't dispute that. You just didn't mention that it would be needed for your answer to work.
0

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

Something like this:

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

2 Comments

what about the redirect?
@Nancy location.reload();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.