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

jjmaster's avatar

Inertia - Vue - Laravel: show skeleton while retreiving data from the backend

Hello:

I was wondering if there is a way to know when the data is being retrieved from the backend?

I am using Inertia + Vue + Laravel and I use the same child component to show some data. I would like to hide the data in the child component (with something like Skeleton), while the data is retrieved from the backend, so it doesn't show the previous data.

I am retrieving the data through an Inertia Link.

Thanks a lot.

0 likes
10 replies
LaryAI's avatar
Level 58

One solution to this problem is to use a loading state in your component and conditionally render the skeleton or the actual data based on whether the data has been loaded or not. Here's an example:

  1. In your child component, define a loading state:
<template>
  <div>
    <div v-if="loading"> <!-- show skeleton -->
      <Skeleton />
    </div>
    <div v-else> <!-- show actual data -->
      <h1>{{ data.title }}</h1>
      <p>{{ data.description }}</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: true,
      data: null,
    };
  },
  mounted() {
    // fetch data from backend
    this.$inertia.get('/api/data').then((response) => {
      this.data = response.data;
      this.loading = false; // set loading state to false when data is loaded
    });
  },
};
</script>
  1. In your Laravel controller, return a response with a delay to simulate a slow backend:
public function getData()
{
    sleep(2); // simulate a slow backend
    return response()->json([
        'title' => 'Some title',
        'description' => 'Some description',
    ]);
}

With this setup, the child component will show the skeleton while the data is being retrieved from the backend, and then show the actual data once it's loaded. You can adjust the delay in the Laravel controller to simulate a slower or faster backend.

MohamedTammam's avatar

If the component is showed, the data is retrieved. That's how Inertia works.

Can you please give a use case that show the component without data retrieved from the back-end?

jjmaster's avatar

Thanks for the answer, Mohamed, and sorry, maybe I didn't explain myself clear enough.

In my component, I have a prop that is filled with the information from Laravel (information sent through Inertia::render), everytime I click on a <Link> (post method).

When I click on that Link and the data is loaded into the prop, next time I click on the Link, while retrieving the data, I want the previous data to be hidden.

In order to do so, I need to know that the data is being retrieved to show a Skeleton in the meantime.

Thanks.

1 like
jjmaster's avatar

Thanks for your answer @mohamedtammam . But what if I need several buttons pointing to different endPoints? Could I add more than just one form in the same component?

jjmaster's avatar

@MohamedTammam thanks. Would you mind pointing me to the documentation where I can create more than one form in the same component, please?

Thanks.

jjmaster's avatar

@MohamedTammam I know how to use the processing part, but I cannot find how to have more than just one form per component.

Please or to participate in this conversation.