How to show loading animations or progress bars when retrieve data from database?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# How to Show Loading Animations While Retrieving Data from the Database As developers, we all understand the frustration of waiting. When a user clicks a button expecting data, silence feels like failure. If a database query takes several seconds—especially when retrieving large datasets, like your example of 15,000 rows—the lack of feedback leads to poor User Experience (UX). The core challenge here is managing the perceived latency. We need a mechanism to signal to the user that a process is ongoing, preventing them from thinking the application has frozen. This isn't just about showing a spinning wheel; it’s about architecting an asynchronous experience. This guide will walk you through the most efficient and developer-friendly ways to implement loading feedback when dealing with long-running database operations, focusing on modern front-end practices that pair well with robust back-end frameworks like Laravel. --- ## The Philosophy: Asynchronous Feedback The most efficient way to handle data retrieval delays is by leveraging asynchronous communication (AJAX or Fetch API). When you initiate a request to your server to fetch data, the browser can immediately display a loading indicator *before* the response arrives. Once the response is received—whether it's successful data or an error—the loading indicator is hidden. For heavy operations, we need to consider three phases: 1. **Initiation:** Show the loader immediately upon clicking the request button. 2. **Processing:** Keep the loader visible while the server executes the slow database query. 3. **Completion:** Hide the loader and display the results (or an error message). ## Method 1: The Standard AJAX/API Approach When retrieving data, moving away from traditional page reloads to using APIs is crucial. This allows for granular control over the loading state. ### Back-end Setup (The Server Side) Your server (e.g., a Laravel controller) initiates the query. If this operation is truly long-running (>3 seconds), you should use background job processing (like Laravel Queues) rather than blocking the HTTP request thread entirely. However, for immediate feedback, we focus on signaling the wait time. ### Front-end Implementation (The Client Side) We use JavaScript to manage the state changes based on the network request status. **HTML Structure:** First, set up a container where your animation will live: ```html
Loading data...
``` **JavaScript Logic (Conceptual Example):** When the user triggers the load, you immediately show the loader and disable interaction: ```javascript function fetchData(endpoint) { const loadingIndicator = document.getElementById('loading-indicator'); const resultsDiv = document.getElementById('results'); // 1. Show Loader & Disable Interaction loadingIndicator.style.display = 'block'; resultsDiv.innerHTML = ''; // Clear previous results // Start the AJAX call (e.g., using Fetch or Axios) fetch(endpoint) .then(response => response.json()) .then(data => { // 3. Completion: Hide Loader and display data loadingIndicator.style.display = 'none'; resultsDiv.innerHTML = renderData(data); // Function to draw the table/content }) .catch(error => { // Handle errors loadingIndicator.style.display = 'none'; resultsDiv.innerHTML = '

Error loading data.

'; console.error('Fetch error:', error); }); } // Call this function when the user clicks the button document.getElementById('load-button').addEventListener('click', () => { fetchData('/api/data'); }); ``` ## Method 2: Handling Massive Datasets (Pagination and Streaming) Retrieving 15,000 rows in a single request is inefficient, regardless of the loading animation. The database should never be asked to send millions of records at once. For large datasets, the best practice involves **Pagination** and **Server-Side Processing**. Instead of fetching all data at once, you implement logic on the backend that limits the results returned per page (e.g., 50 rows). When using Laravel, Eloquent's built-in pagination methods make this straightforward: ```php // In your Laravel Controller public function index() { // Fetch only 50 records at a time $data = YourModel::paginate(50); return response()->json($data); } ``` When the front-end requests data, it only waits for those 50 records. The loading animation now signals the *page loading*, not the entire data dump, making the wait time feel much shorter and more manageable. This pattern is fundamental to building scalable applications on a platform like **Laravel**. ## Conclusion Showing loading animations is a matter of managing user expectations through asynchronous design patterns. By implementing robust AJAX calls and focusing on efficient data retrieval methods like pagination, you transform a frustrating wait into an informative experience. Remember, the goal isn't just to show *something* is happening; it’s to show *what* is happening and *why* the user should wait. By combining thoughtful front-end feedback with optimized back-end querying, you create applications that are both fast and delightful to use.