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

mdmohammudullaha's avatar

How can i get dynamic data from backend in edit form when click edit button on vue

Hi, I'm Using Laravel with Vue and inertia. I rendered a list with 'Users' table data. And every row has a button called 'Edit'. So, the concept is when click on edit button a popup will open with name, email, password. I want to prefill (name, email) that form with the user data I'm editing. So how and from where I can get that data. Specially I'm thinking to fetch the correspond user data from backend as list may not contain detailed data. How can I do this in Vue with inertia. I tried to use Vue manual visits this change the URL when fetch the data. I'm new in vue. I also need a little concept where should I store that data if I fetch from backend.

0 likes
2 replies
LaryAI's avatar
Level 58

To prefill the form with the user data, you can pass the user data as a prop to the edit component and use it to set the initial values of the form fields. Here's an example:

  1. In your Laravel controller, fetch the user data and pass it to the edit view:
public function edit(User $user)
{
    return Inertia::render('EditUser', [
        'user' => $user
    ]);
}
  1. In your Vue component, define a prop to receive the user data:
export default {
  props: {
    user: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      form: {
        name: this.user.name,
        email: this.user.email,
        password: ''
      }
    }
  }
}
  1. Use the prop to set the initial values of the form fields:
<template>
  <div>
    <input type="text" v-model="form.name">
    <input type="email" v-model="form.email">
    <input type="password" v-model="form.password">
  </div>
</template>
  1. Pass the user data prop to the edit component when rendering it:
<template>
  <div>
    <button @click="editUser(user)">Edit</button>
    <EditUser :user="selectedUser" />
  </div>
</template>

<script>
import EditUser from './EditUser.vue'

export default {
  components: {
    EditUser
  },
  data() {
    return {
      users: [],
      selectedUser: null
    }
  },
  methods: {
    editUser(user) {
      this.selectedUser = user
    }
  }
}
</script>

Now when you click the Edit button, the EditUser component will receive the user data as a prop and use it to prefill the form fields.

mdmohammudullaha's avatar

@LaryAI Thanks AI. But I'm rendering the edit component on the same page as popup where the user list is. And Every time I click on edit button, I have to open the popup with targeted user data.

Please or to participate in this conversation.