Laravel Datatables Filter with Dropdown

Step 1: Install the laravel project using the command line.

composer create-project --prefer-dist laravel/laravel laravel7filter

Step 2: Open project in code and use vs code terminal.

Step 3: find .env file in root directory than setup database configuration. 


Step 4 : Install Yajra Datatable

composer require yajra/laravel-datatables-oracle

Step 5: After that you need to set providers and alias.


'providers' => [
'aliases' => [
	'DataTables' => Yajra\DataTables\Facades\DataTables::class,

Step 6 : Add Status Column in Users Table

php artisan make:migration add_status_column


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddStatusColumn extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::table('users', function (Blueprint $table) {
     * Reverse the migrations.
     * @return void
    public function down()

Now we can run our migration:

php artisan migrate

Step 7: Add Dummy Records

php artisan tinker
factory(App\User::class, 100)->create();

Step 8: Add Route

Route::get('users', ['uses'=>'[email protected]', 'as'=>'users.index']);

Step 9: Create Controller


namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use DataTables;
class UserController extends Controller
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function index(Request $request)
        if ($request->ajax()) {
            $data = User::select('*');
            return Datatables::of($data)
                    ->addColumn('status', function($row){
                            return '<span class="badge badge-primary">Active</span>';
                            return '<span class="badge badge-danger">Deactive</span>';
                    ->filter(function ($instance) use ($request) {
                        if ($request->get('status') == '0' || $request->get('status') == '1') {
                            $instance->where('status', $request->get('status'));
                        if (!empty($request->get('search'))) {
                             $instance->where(function($w) use($request){
                                $search = $request->get('search');
                                $w->orWhere('name', 'LIKE', "%$search%")
                                ->orWhere('email', 'LIKE', "%$search%");
        return view('users');

Step 10: Create View

<!DOCTYPE html>
    <title>Laravel Datatables Filter with Dropdown</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="stylesheet" href="" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link href="" rel="stylesheet">
<div class="container">
    <div class="jumbotron text-center">
            <h1>Laravel Datatables Filter with Dropdown</h1>
    <div class="card">
        <div class="card-body">
            <div class="form-group">
                <label><strong>Status :</strong></label>
                <select id='status' class="form-control" style="width: 200px">
                    <option value="">Select Status</option>
                    <option value="1">Active</option>
                    <option value="0">Deactive</option>
  <table class="table table-bordered data-table">
                <th width="100px">Status</th>
<script src="" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<script src=""></script>
<script src=""></script>
<script src="" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src=""></script>   
<script type="text/javascript">
  $(function () {
    var table = $('.data-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
          url: "{{ route('users.index') }}",
          data: function (d) {
                d.status = $('#status').val(),
       = $('input[type="search"]').val()
        columns: [
            {data: 'id', name: 'id'},
            {data: 'name', name: 'name'},
            {data: 'email', name: 'email'},
            {data: 'status', name: 'status'},


Now we are ready to run our example so run bellow command ro quick run:

php artisan serve

Now you can open bellow url on your browser:
Leave a Comment