Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ufodisko's avatar

Pass ID from Inertia to Laravel Controller

I have a view where I'm displaying a form for data submissions (textarea and submit button) and a list of the user's latest posts (limited to 5) underneath it.

Each post has a button to save/favorite it to user's account.

The problem is that I am unable to pass the post ID on the post request.

I tried to pass the value of a hidden field that is populated by the post id, but it didn't work.

App\Models\User::favorite(): Argument #1 ($object) must be of type Illuminate\Database\Eloquent\Model, null given, called in C:\wamp64\www\write\app\Http\Controllers\PostController.php on line 70

All I need to do is pass post.id inside the vue loop to the save() method in PostController.php

Controller

public function index()
{
    $posts = Post::where('user_id', '=', Auth::id())
                    ->limit(5)
                    ->orderBy('created_at','desc')
                    ->get();

    return Inertia::render('Post', [
        'posts' => $posts
    ]);
}

public function save(Request $request) {
    $user = User::find(Auth::id());
    $post = $request->input('postId');

    $user->favorite($post);

if($user) {
    return redirect()->back()->with('message', 'Post Saved!');
} else {
    return 'something went wrong';
}

}

HTML

<form @submit.prevent="" class="d-flex justify-content-between gap-3">
    <div class="row w-100">
        <!-- <input type="text" class="form-control" name="inputText" id="inputText"
            v-model="form.inputText"> -->

        <div class="col-12">
            <textarea class="form-control" name="inputText" id="inputText"
                placeholder="start writing..." rows="3" v-model="form.inputText"></textarea>
        </div>

        <div class="col-12 mt-2">
            <button type="submit" class="btn btn-primary"
                @click.prevent="post(form)">Post</button>
        </div>
    </div>
</form>

<div class="posts gap-3">
    <div class="card w-100 bg-light mt-4 mb-4" v-for="(post, index) in $page.props.posts"
        :key="post.id">
        <div class="card-body" :data-id="post.id">
            <h5 class="card-title">{{ post.request }}</h5>
            <p class="card-text">{{ post.response }}</p>
            <div class="buttons gap-3">
                <a href="#" class="btn btn-outline-secondary btn-sm mx-2 copy"
                    @click.prevent="copyToClipBoard(post.response); copyBtn(index, $event)">
                    <i class="fa-regular fa-copy"></i>
                    {{ copyBtnText }}
                </a>
                <form @click.prevent="" method="POST">
                    <button type="submit" class="btn btn-outline-secondary btn-sm save"
                        @click.prevent="save(postID)">
                        <i class="fa-regular fa-heart"></i> Save</button>
                    <input type="hidden" name="postId" id="postId" :value="post.id">
                </form>

            </div>
        </div>
    </div>
</div>

Javascript

import { defineComponent } from "vue"
import { reactive } from 'vue'
import AppLayout from "@/Layouts/AppLayout.vue"

export default defineComponent({
    components: {
        AppLayout
    },
    props: ['errors'],
    data() {
        return {
            form: {
                inputText: null,
            },
            copyBtnText: 'Copy',
            postID: 7
        }
    },
    methods: {
        post(data) {
            this.$inertia.post('/post', data)
        },
        save(s) {
            this.$inertia.post('/save', s)
        },
        copyToClipBoard(textToCopy) {
            navigator.clipboard.writeText(textToCopy);
        },
        copyBtn(index, e) {
            // this.copyBtnText = 'Copied!'
            e.target.innerText = 'Copied!'
        }
    }
});
0 likes
8 replies
tykus's avatar
tykus
Best Answer
Level 104

So... right now your form is doing nothing (the event hook on the form should be @submit.prevent, not @click.prevent). Instead, there is a click event handler on the button which passes postID to a save method, and in the save method, you are POSTing to a /save endpoint; but the payload is not an Object, so there is no label on the data received in the Laravel Controller:

save(s) {
    this.$inertia.post('/save', {postId: s})
},

TBH, there are a lot of changes I would suggest, but this will turn into one of "those" threads if I did...

1 like
ufodisko's avatar

I'm sorry about the mess but I've been trying many solutions and I gave up in frustration.

All I want to do is pass post.id from the view to the controller so I can catch it with $post

Your solution did not make any difference.

ufodisko's avatar

@tykus return $request->all() returns the actual id of the post.

{"postID":7}

In the controller I have

 $post = $request->input('postId');`

and im passing the id from a hidden field

 <input type="hidden" name="postId" id="postId" :value="post.id">

when returning the value before saving to db, I get the post id, when I save to db I get an error.

tykus's avatar

@ufodisko

im passing the id from a hidden field

No, you're not - that's not how forms work in an Inertia app - https://inertiajs.com/forms

return $request->all() returns the actual id of the post.

Show me the actual output

ufodisko's avatar

@tykus your solution was correct after all. The issue is fixed.

Sorry for the mess of a code, it's my first time working with inertia and vue in laravel and some concepts are still fuzzy.

The solution was very simple

// Vue
<button type="submit" @click.prevent="save(prompt.id)">
this.$inertia.post('/save', { post_id: s })

// Controller
$post = Post::find($request->post_id);
 $user->toggleFavorite($post);
tykus's avatar

@ufodisko the button can be a button type if it is not inside a form:

<button type="button" @click="save(prompt.id)">

Otherwise, mark the thread solved if you're all set

1 like

Please or to participate in this conversation.