Get info by javascript in laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Dynamic Data Fetching: Getting Info by JavaScript in Laravel
As senior developers, we frequently encounter scenarios where the frontend needs dynamic data sourced from complex backend logic—often involving multiple database tables and conditional filtering. The scenario you described—fetching a specific price (amount) based on a product ID and a quantity range—is a classic example of this challenge. While pure JavaScript handles the presentation, the heavy lifting of accurate filtering must occur on the server side, within your Laravel application.
This post will walk you through the logical pitfalls in your current implementation and provide a robust, efficient, and idiomatic way to structure your Laravel backend and JavaScript frontend to achieve dynamic data retrieval seamlessly.
The Core Challenge: Relational Data Filtering
Your goal is to link information across two tables: products (to get the base price) and discounts (to find the applicable price based on a quantity range). Relying solely on complex, chained SQL logic directly in your JavaScript AJAX calls is brittle. The best practice is to let Laravel handle the database query, ensuring data integrity and security.
The key challenge lies in correctly translating the front-end request parameters (Product ID, Quantity Min, Quantity Max) into a precise database filter that resolves to a single amount.
Backend Strategy: Laravel Eloquent for Precision
Instead of trying to use complex WHERE clauses on raw database tables within your controller, we should leverage Laravel’s Eloquent ORM. This makes the code cleaner, less prone to SQL injection, and easier to maintain.
1. Redefining the Route
The route should clearly define what parameters are being requested. We need the product ID and the quantity range (min and max).
// routes/web.php
Route::get('qtydisc/{productId}/price_by_quantity', 'ProductController@getPriceByQuantity');
2. Implementing the Controller Logic
The controller method must accept these parameters and use Eloquent relationships or direct query methods to find the correct record efficiently. A common mistake is trying to filter based on ranges using simple inequality operators; you need compound conditions.
Here is how you would structure the logic in your controller:
// app/Http/Controllers/ProductController.php
use App\Models\Discount; // Assuming you have a Discount model
use Illuminate\Support\Facades\DB;
public function getPriceByQuantity($productId, $minQty, $maxQty)
{
// Ensure the inputs are treated as integers for comparison
$min = (int)$minQty;
$max = (int)$maxQty;
// Use the Query Builder to find the specific discount amount.
// We look for a record where:
// 1. product_id matches the requested ID
// 2. The quantity range [min, max] is inclusive of the desired quantity.
$discount = DB::table('qty_discounts')
->where('product_id', $productId)
->where('min', '<=', $max) // Ensure min is less than or equal to max (sanity check)
->where(function ($query) use ($min, $max) {
// The quantity requested must fall within the discount range [min, max]
$query->where('min', '<=', $min)
->where('max', '>=', $max);
})
->select('amount')
->first();
if (!$discount) {
return response()->json(['error' => 'No matching discount found for this quantity range.'], 404);
}
return response()->json($discount);
}
Why this is better: This approach ensures that the database only returns a result if the requested quantity falls within the defined min and max bounds specified in the discount table, solving your issue where you were getting incorrect results (e.g., getting 7000 instead of 5000).
Frontend Implementation: Clean AJAX Calls
The JavaScript should focus purely on gathering the necessary data from the DOM and making a single, clean request to the API endpoint defined above. It should not attempt to perform complex mathematical logic itself.
// Example structure for your JavaScript function
function calculatePrice() {
var productId = document.getElementById("productId").value; // Get product ID from context
var quantity = parseInt(document.getElementById("quantity").value); // The user's input quantity
var minQty = document.getElementById("min_qty").value; // Quantity range start
var maxQty = document.getElementById("max_qty").value; // Quantity range end
if (!productId || !minQty || !maxQty) {
return; // Stop if essential data is missing
}
$.ajax({
url: '{{ url('admin/qtydisc') }}/' + productId + '/price_by_quantity', // Use the new structured route
type: "GET",
dataType: "json",
success: function(data) {
if (data && data.amount) {
// 'data.amount' now holds the correct price dynamically calculated by Laravel
var finalPrice = data.amount;
console.log('Calculated Price:', finalPrice);
$('#totalPriceInTotal').text('Rp ' + finalPrice);
} else {
$('#totalPriceInTotal').text('Error: Could not calculate price.');
}
},
error: function(xhr) {
console.error("AJAX Error:", xhr.responseText);
$('#totalPriceInTotal').text('An error occurred during calculation.');
}
});
}
$(document).ready(function() {
// Attach the function to listen for changes on quantity and range fields
$('#quantity').on('change', calculatePrice);
// You would need similar listeners for min_qty and max_qty inputs as well.
});
Conclusion
Getting dynamic data across tables requires a clear separation of concerns: Laravel handles the data (the "what"), and JavaScript handles the presentation (the "how"). Your initial difficulty stemmed from trying to force complex relational filtering into the client-side AJAX call. By structuring your Laravel route and controller to perform precise, multi-conditional lookups using Eloquent or the Query Builder, you create a stable API endpoint that delivers accurate results, making your frontend development much more reliable. Remember, embracing the power of the backend for data integrity is the cornerstone of building robust applications on the Laravel framework.