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

rfmapp's avatar

Laravel/VUE: show report to user dynamically

I have this page where a user can select multiple facilities to pull data from, like manufactured units, that are presented in a report. Right now it works perfectly: I send the list of facilities, loop through each, call an API endpoint to make sure I have the most recent data, create/update the database if necessary, get data from the database based on some criteria, add each returned data to a new array and return the array to frontend. While the loop is happening, the front end shows a page with the report header and a loading indication.

Now, what I have been tasked with is: would be possible to present data to the user after each loop iteration? Let's say the user selected 5 facilities: how could I show data for the 1st facility in that report page, keep the loading indication, then update it with the second iteration result, and so on? I'm not a front-end guy, just joined the team, and mostly done work not using these frontend frameworks, so, be patient with me guys ;D

I have a feeling that the backend code does not need to change, the front end (using VUE) is where the refactoring needs to happen (send each facility request in a loop, maybe?).

If anyone can give me some directions, a simple example, or anything that I can use as a starting point to refactor the front-end code (if my assumption is correct), it would be greatly appreciated.

0 likes
8 replies
LaryAI's avatar
Level 58

Certainly! To achieve the functionality where the frontend updates the report with data after each loop iteration, you can use Vue.js to make asynchronous calls to your Laravel backend for each facility and update the report as each call resolves. Here's a high-level solution:

  1. In your Vue component, you'll need to manage the state for the facilities, the loading status, and the report data.
  2. You'll iterate over the selected facilities and make an API call for each one.
  3. After each API call resolves, you'll update the report data with the new information and re-render the component.

Here's a simplified example of how you might implement this in Vue:

<template>
  <div>
    <h1>Facility Report</h1>
    <div v-for="facility in reportData" :key="facility.id">
      <!-- Render your facility report data here -->
      <p>{{ facility.name }}: {{ facility.data }}</p>
    </div>
    <div v-if="loading">
      Loading...
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      facilities: [], // This should be populated with the selected facilities
      reportData: [],
      loading: false,
    };
  },
  methods: {
    async fetchFacilityData() {
      this.loading = true;
      for (let facility of this.facilities) {
        try {
          const response = await this.$http.get(`/api/facility/${facility.id}/data`);
          // Assuming the response contains the facility data
          this.reportData.push({
            id: facility.id,
            name: facility.name,
            data: response.data,
          });
        } catch (error) {
          console.error('Error fetching facility data:', error);
        }
      }
      this.loading = false;
    },
  },
  created() {
    this.fetchFacilityData();
  },
};
</script>

In this example, this.$http.get is a placeholder for whatever HTTP client you're using (e.g., axios). Replace it with the actual method you use to make HTTP requests.

The fetchFacilityData method loops through each facility, makes an API call, and then updates the reportData array with the results. The loading property is used to show a loading indicator while the data is being fetched.

Remember to handle errors appropriately and consider edge cases where data might not be available or the request fails.

This is a basic example to get you started. Depending on your application's complexity, you might need to add more features, such as error handling, loading states for individual facilities, or even using Vuex for state management if your application is large.

1 like
gych's avatar

So you're already looping the selected facilities and making separate API calls for each facility ?

Then just add the data after each call to the report page?

rfmapp's avatar

@gych yes, today the backend does that, but it returns the whole result to the frontend. What I need to do is to present the data on the frontend as it comes, not to wait for the whole result to be done.

gych's avatar
gych
Best Answer
Level 29

@rfmapp Well if you currently only make one call from the front end to the back end to handle the selected list of facilities it will not be possible. Since all facilities and data is being sent and returned at the same time.

You should make separate calls for each facility on the front end so that the backend only returns one report per facility. Once the report for the facility is returned you can add it to the template.

1 like
rfmapp's avatar

@gych basically, remove the loop from the backend and put it on the frontend, right? And this way, it will be shown as they come and update the frontend with the next set of data?

gych's avatar

@rfmapp Yes indeed this way you can add the returned data to the template after each response.

rfmapp's avatar

@gych Thanks man, I'll start looking into this changes!!

1 like
gych's avatar

@rfmapp No problem, don't forget to close your thread by selecting the best answer. If you have any more questions don't hesitate to reach out :)

Please or to participate in this conversation.