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

vincent15000's avatar

Best way to load a model datas in a modal form

Hello,

I'm trying to compare two ways to load a model datas in a modal form.

I have a table with (for example) books and for each category I have a button to edit it. Furthermore I have a modal form. This is my Books/Index.vue page.

When I click on the button, I have two options to code how the modal form will be filled :

  • eiter I refresh the Books/Index.vue page, I load only the books (Inertia::render(..., only: 'book', ...)) and I access to the book datas with usePage()
  • or I pass the bookId to the modal form and the modal form send a simple axios request to retrieve the book datas

I know that I have already asked similar questions in previous posts (similar but not exactly the same) and this is a real problem for me because modal forms is not how InertiaJS works.

IMHO I think that it's cleaner to use a simple axios request for the modal form and keep the InertiaJS request to refresh the table.

One more question : if I visit the same page again with an InertiaJS request by specifying for example only books, I have understood that the controller will only reload the books list and not the other datas which will be retrieved from the state. But in the frontend part of the code, does the entire page refresh (with the menu and other data from the state) ? Or only the books table ?

I have perhaps an idea of the answer : the entire page will be reloaded, retrieving the datas from the controller (if reloaded) or from the state (if not reloaded). If this is right, the best way to load datas in my modal form is perhaps the axios way.

What do you think about all this ?

Thanks for your help.

V

0 likes
9 replies
martinbean's avatar
Level 80

@vincent15000 If you’re using Vue.js then it sounds like you already have the models in memory as a prop or something? So if you’re iterating over a collection of models, just pass the specific model to the modal as data:

<tr v-for="book in books">
    <td>{{ book.name }}</td>
    <td class="actions">
      <ul>
        <li><a href="showModal(book)">Edit</a></li>
      </ul>
    </td>
</tr>
export default {
  methods: {
    showModal(book) {
      // Open modal here, and pass book as prop or something
    }
  },
};
1 like
vincent15000's avatar

@martinbean Is it possible to pass my model as a prop inside a method ? Usually I set the id of the model to update in the component.

<Form
	:id="itemId"
>
</Form>

And I set the value of the id in the method.

vincent15000's avatar

@martinbean Is it better to :

  • load the datatable page with a modal form and fill the modal by revisiting the page and loading only the model and retrieve the datas with usePage()

  • or load the datatable page with a modal form and let the modal retrieve its datas via an axios request

martinbean's avatar

or load the datatable page with a modal form and let the modal retrieve its datas via an axios request

@vincent15000 This completely defeats the point of what I suggested in the first place.

You’re iterating over a list of objects. You clearly already have the object. Why make a network request to get something you already have?

You‘re looping over a list to display a table. So just pass the current item to whatever form or modal component on click.

Again:

<tr v-for="book in books" v-bind:key="book.id">
  <!-- You have a book here, because you're looping over some books -->
  <td class="actions">
    <!-- So just pass the book instance as an argument to a method -->
    <button v-on:click="viewBook(book)">View</button>
  </td>
</tr>
methods: {
  viewBook(book) {
    // The book argument here is the book in the row you clicked the button
  },
},
1 like
vincent15000's avatar

@martinbean Yes that's ok for me ... but in another case where the table has a very large number of columns, I load in the table only a few columns (and for payload convenience I load only the needed columns), I don't have the entire model.

Perhaps my question is also educational. If I really need to get the model via a request, is there a best way to do that in the case I use InertiaJS, so with InertiaJS visit or with an axios request.

Or it is perhaps best to always load all the columns in the datatable ?

Please or to participate in this conversation.