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

CamKem's avatar
Level 10

Using eloquent relationships within Vue/Inertia

Hello, I am just wondering if there is any mechanism to use eloquent relationships inside of Vue/Inertia component in the way they can be used in blade components?

@foreach ($user->messages as $message)
0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

Yes, you can use eloquent relationships within Vue/Inertia components. You can pass the related data as a prop to the component and then access it within the component using Vue's data property.

For example, if you have a User model with a messages relationship, you can pass the messages data to a Vue component like this:

// In your controller
public function show(User $user)
{
    return Inertia::render('UserShow', [
        'user' => $user->load('messages'),
    ]);
}

// In your Vue component
<template>
    <div>
        <h1>{{ user.name }}</h1>
        <ul>
            <li v-for="message in user.messages" :key="message.id">
                {{ message.content }}
            </li>
        </ul>
    </div>
</template>

<script>
export default {
    props: {
        user: Object,
    },
    data() {
        return {
            // You can access the messages data here
        };
    },
};
</script>

In this example, we're passing the user data (including the messages relationship) to the UserShow component as a prop. Within the component, we can access the user data using this.user, and we can access the messages data using this.user.messages.

Note that you'll need to make sure to load the related data using Eloquent's load method before passing it to the component.

3 likes

Please or to participate in this conversation.