Displaying dynamic tables with search, pagination, and sorting is a must-have in modern Laravel applications. While the popular yajra/laravel-datatables
package simplifies this task, it may not suit every need. In this blog, we’ll show you how to use DataTables.js in Laravel 12 with a fully custom backend using raw PHP and Eloquent — no external packages like Yajra required.
🧰 What You’ll Learn
- Setup Laravel project and database
- Create models, migrations, and dummy data
- Build a controller with custom server-side AJAX pagination
- Integrate DataTables.js on the frontend
- Search, sort, and paginate records without any external Laravel package
🚀 Prerequisites
Make sure you have:
- PHP 8.1+
- Composer
- Laravel 12
- MySQL or any database
- Basic Laravel knowledge
📁 Step 1: Create Laravel 12 Project
If you haven't already, create a new Laravel project:
🔧 Step 2: Create Model and Migration
We will use a Barcode
model with relations to a Product
.
Run the model and migration:
database/migrations/xxxx_xx_xx_create_products_table.php
database/migrations/xxxx_xx_xx_create_barcodes_table.php
Then run:
Seed with dummy data (you can use factories or insert manually).
👨💻 Step 3: Define Routes
Open routes/web.php
and add:
🧠 Step 4: Create Controller with Custom DataTables Response
Generate the controller:
Now use your custom method. Replace the contents of app/Http/Controllers/BarcodeController.php
:
🖼 Step 5: Create Blade View with DataTable Integration
Create the view at resources/views/barcodes/index.blade.php
:
🔁 How It Works
- The front-end table calls
/barcodes/data
using AJAX. - Your controller manually handles:
- Pagination
- Searching
- Sorting
- The result is formatted as JSON as per DataTables spec.
📌 Pros of Not Using Yajra
Feature | Advantage |
---|---|
Zero external dependency | Pure Laravel & JS |
More control | Custom query logic |
Leaner app | Lightweight |
Easier debugging | All logic visible in controller |
🛑 Common Issues
Problem | Fix |
---|---|
No data loads | Check network tab and browser console |
500 server error | Check your controller code or route config |
Image not displaying | Ensure barcode_image_url is a valid path |
📁 Final Folder Structure
📌 Conclusion
You now have a complete working example of how to integrate DataTables.js in Laravel 12 with fully custom server-side pagination, search, and sorting — without any external packages like Yajra.
This approach is perfect for those who:
- Want lightweight apps
- Prefer custom query logic
- Don’t want to rely on packages
📦 Bonus Tips
- Add edit/delete buttons with modals
- Use buttons extension for export to Excel/PDF
- Add column filters
- Lazy-load product name with eager loading
💬 Have Questions?
Let me know in the comments, or reach out if you want the full code in a downloadable ZIP or need help deploying it to production.