Hello, web developers! In this article, we'll see filtering a DataTable using a dropdown in Laravel 10. I'll give you step-by-step information about how to filter datatable using the dropdown in laravel 8, laravel 9, and laravel 10.
Here, we'll install yajra/datatable composer package and also create a custom dropdown filter for datable in laravel 10.
In this step, we'll install the laravel 10 application using the following command.
composer create-project --prefer-dist laravel/laravel your-project-name
Then, we'll configure the database connection in the .env
file, and run migrations to create necessary tables.
php artisan migrate
Next, we'll create a model using the following command.
php artisan make:model Item -m
This command will also create a migration file.
In the generated migration file (database/migrations/YYYY_MM_DD_create_items_table.php
), define the columns for your table. Then, run the migrations.
php artisan migrate
If you want sample data, create a seeder for your model:
php artisan make:seeder ItemSeeder
database/seeders/ItemSeeder.php
php artisan db:seed --class=ItemSeeder
Install the DataTables package using Composer:
composer require yajra/laravel-datatables-oracle
In your controller, fetch the data and pass it to the DataTable.
app/Http/Controller/ItemController.php
<?php
namespace App\Http\Controllers;
use App\Models\Item;
use Illuminate\Http\Request;
use DataTables;
class ItemController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
if ($request->ajax()) {
$data = Item::select('*');
return Datatables::of($data)
->addIndexColumn()
->addColumn('approved', function($row){
if($row->approved){
return '<span class="badge badge-primary">Yes</span>';
}else{
return '<span class="badge badge-danger">No</span>';
}
})
->filter(function ($instance) use ($request) {
if ($request->get('approved') == '0' || $request->get('approved') == '1') {
$instance->where('approved', $request->get('approved'));
}
if (!empty($request->get('search'))) {
$instance->where(function($w) use($request){
$search = $request->get('search');
$w->where('name', 'LIKE', "%$search%")
->orWhere('description', 'LIKE', "%$search%")
->orWhere('price', 'LIKE', "%$search%");
});
}
})
->rawColumns(['approved'])
->make(true);
}
return view('index');
}
}
Create a Blade file and include the DataTable initialization script.
resources/views/items/index.blade.php
<html>
<head>
<title>Laravel 10 Filter Datatable with Dropdown - Vidvatek</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
<link href="https://cdn.datatables.net/1.10.25/css/dataTables.bootstrap5.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.js"></script>
<script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.25/js/dataTables.bootstrap5.min.js"></script>
</head>
<body>
<div class="container">
<h1>Laravel 10 Filter Datatable with Dropdown - Vidvatek</h1>
<div class="card">
<div class="card-body">
<div class="form-group">
<label><strong>Approved :</strong></label>
<select id='approved' class="form-control" style="width: 200px">
<option value="">Approved</option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
</div>
</div>
<table class="table table-bordered data-table">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th width="100px">Approved</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</body>
<script type="text/javascript">
$(function () {
var table = $('.data-table').DataTable({
processing: true,
serverSide: true,
ajax: {
url: "{{ route('items.index') }}",
data: function (d) {
d.approved = $('#approved').val(),
d.search = $('input[type="search"]').val()
}
},
columns: [
{data: 'id', name: 'id'},
{data: 'name', name: 'name'},
{data: 'description', name: 'description'},
{data: 'price', name: 'price'},
{data: 'approved', name: 'approved'},
]
});
$('#approved').change(function(){
table.draw();
});
});
</script>
</html>
Define a route in web.php to load the view.
routes/web.php
Route::get('/items', 'ItemController@index')->name('items.index');
Run the laravel 10 application using the following command.
php artisan serve
You might also like:
- Read Also: How To Add Export Button In Datatable
- Read Also: Laravel 10 Vue 3 Search Filter with Pagination
- Read Also: Building Complete CRUD Application in Laravel 10
- Read Also: AJAX CRUD Operations In Laravel 10: Step-by-Step Guide