laravel 5 simple ajax retrieve record from database
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Mastering AJAX in Laravel 5: Retrieving Database Records Seamlessly
As a senior developer, I often see developers stumbling when bridging the gap between frontend interactivity (AJAX) and backend structure (Laravel MVC). You have the perfect database logic in your controller, and you have working JavaScript for fetching data, but connecting them requires understanding how Laravel handles HTTP requests.
The challenge you are facingâretrieving data via AJAX after a form submission or selectionâis fundamentally about correctly setting up the communication pipeline: Frontend $\rightarrow$ Route $\rightarrow$ Controller $\rightarrow$ Response.
This guide will walk you through exactly how to wire your existing HTML, JavaScript, and Eloquent logic together in Laravel 5 to successfully retrieve employee records based on a branch selection using AJAX.
## Understanding the MVC Flow for AJAX Data Retrieval
When you use AJAX, you are essentially making an asynchronous HTTP request from the browser to your server. In Laravel, this request must be handled by a defined route that maps to a specific controller method, which then executes your business logic (the Eloquent query) and returns the result, typically formatted as JSON.
Here is the complete flow we need to establish:
1. **Frontend (HTML/JS):** The user selects an item (e.g., `branch1`), and JavaScript triggers an AJAX POST request containing the selected ID.
2. **Route Definition (`web.php`):** We define a specific URL endpoint that listens for this incoming POST request and directs it to our controller method.
3. **Controller Logic:** The controller receives the data, uses it to query the database via Eloquent (exactly as you designed), and packages the results into a JSON response.
4. **Response:** Laravel sends the JSON data back to the browser, which JavaScript then parses and updates the HTML.
## Step 1: Defining the Route in `web.php`
First, we need a route that accepts the POST request from your AJAX call. Let's assume you are using resource-style routing or simple routes in your `routes/web.php`. We will map this to a method in a hypothetical `BranchController`.
```php
// routes/web.php
use App\Http\Controllers\BranchController;
// Route to handle the AJAX request and retrieve employee data
Route::post('/get-employees', [BranchController::class, 'getEmployeeData'])->name('employee.data');
```
## Step 2: Implementing the Controller Logic
Now we implement the method in your controller that will execute the query you already wrote. This method must accept the input from the request and return the data as a JSON response.
In your `app/Http/Controllers/BranchController.php`:
```php
input('id'); // Get the 'id' sent by the AJAX data
if (!$branchNo) {
return response()->json(['error' => 'Branch ID is missing'], 400);
}
// 2. Execute the Eloquent query using the received ID
$employees = Employees::where('branch_no', $branchNo)->get();
// 3. Return the results as a JSON response
return response()->json($employees);
}
}
```
Notice how we use `$request->input('id')` to safely retrieve the data sent by the AJAX call and `response()->json($employees)` to format the output correctly for the browser. This practice of using JSON responses is fundamental when building modern, API-driven applications, which aligns perfectly with the principles promoted by platforms like https://laravelcompany.com.
## Step 3: Refining the JavaScript (AJAX)
Finally, we need to adjust your JavaScript to point to the correct route and handle the JSON response properly. We will use the `data` property of the AJAX call to send the ID, and check the `success` function for the returned data.
Assuming your HTML structure remains as provided:
```html
```
The updated JavaScript should look like this:
```javascript
$("#branchname").change(function(){
var branchId = $(this).val(); // Get the selected ID (e.g., '1', '2', '3')
$.ajax({
url: "{{ route('employee.data') }}", // Use the named route defined earlier
type: "POST",
data: { id : branchId }, // Send the branch ID
dataType: 'json', // Expect JSON back
success: function(data){
// data will be an array of employee objects returned from the controller
$("#employees").html(data.map(employee => `${employee->name} ${employee->position} `).join(''));
},
error: function(xhr, status, error) {
console.error("AJAX Error:", status, error);
$("#employees").html('
Error retrieving data.
'); } }); }); ``` ## Conclusion Retrieving dynamic data via AJAX in a Laravel application is all about correctly managing the HTTP contract between your frontend and backend. By defining a clear POST route, ensuring your controller safely processes input, and returning structured JSON data, you create a robust and scalable system. Remember, every request flows through defined endpoints; mastering this flow is key to building powerful applications on the Laravel framework. For more insights into structuring modern web applications, check out the resources available at https://laravelcompany.com.