How to filter database records using dropdown in Laravel

In this post we will learn how to filter database records using dropdown.


Step 1: Install Laravel and Create Application

Step 2: Install Yajra datatable package

Step 3: Config database credentials in .env file

Step 4: Migrate database

Step 5: Create Controller

Step 6: Create blade view file

Step 7: Create Routes

Step 8: Run application

Install Laravel and Create Application: First create new application using below mention composer command.

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

Get in application

cd filterdata

Install Yajra datatable package

Now, We have to install Yajra datatable Package, run below mention composer command to install Yajra package.

composer require yajra/laravel-datatables-oracle

Now config/inject Yajra class inside providers and alias array using config/app.php

    'providers' => [

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

Config database credentials in .env file

Now config database credentials and run migrate command


For filter data we have to add one column in users table “status” column, create a one migration file for add status column using below artisan command.

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()

Run migrate artisan command for migrate your tables in database

php artisan migrate

Add some dummy data

We have to add some dummy data for testing filter, we can use seeder,factory and tinker

php artisan tinker

Create Controller:

Now create controller for write our logic inside controller function, using below mention atisan command we can create controller

php artisan make:controller UserController

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;
use DataTables;

class UserController extends Controller
    public function index(Request $request){

        if ($request->ajax()) {
            $data = User::select('*');
            return Datatables::of($data)
                    ->addColumn('status', function($row){
                            return '<span class="btn btn-outline-success mb-0">Active Users</span>';
                            return '<span class="btn btn-outline-danger mb-0">Deactive Users</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');    

Create blade view file

Now create blade view file inside resources/views/users.blade.php

<!DOCTYPE html>
    <title>Filter Database Records -</title>
    <link href="" rel="stylesheet">
    <link href="" rel="stylesheet">
    <script src=""></script>  
    <script src=""></script>
    <script src=""></script>
    <div class="container">
        <div class="bg-light p-4 text-center">
            <h2 class="text-black m-0">Filter Database Records -</h2>
        <div class="card my-2">
            <div class="card-body">
                <div class="form-group">
                    <label><strong>Filter by Status :</strong></label>
                    <select id='status' class="form-control" >
                        <option value="">Select Status</option>
                        <option value="1">Active</option>
                        <option value="0">Deactive</option>
        <table class="table table-bordered data-table my-2">
                    <th>Sr no.</th>
    <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'},

Create route

Create routes inside web.php for accessing your filter page.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!

Route::get('/', function () {
    return view('welcome');


Run Application

php artisan serve

Leave a Reply

Your email address will not be published. Required fields are marked *