Laravel advance search filters with optional fields.

This post explains how to use the advanced filter with Laravel some fields are optional step by step.

Laravel advance filters with optinal value


In this above Image, have many fields and some are optional it's depends on the user.

Step:1 Create a simple form 

 <form action="{{route('PostSearch')}} " method="get">
         <div class="field">
              <label class="label"><strong class="has-text-grey">Eye color </strong></label>
                    <div class="control">
                         <div class="select  is-primary">
                             <select name="eye_color">
                                    <option value="">Not Given</option>
                                    <option value="Blue" >Blue</option>
                                    <option value="Brown" >Brown</option>
                                    <option value="Green">Green</option>
                                    <option value="Hazel" >Hazel</option>
                                    <option value="Grey" >Grey</option>
                                    <option value="Dark brown">Dark brown</option>
                                    <option value="Rather not say" >Rather not say</option>
                                    <option value="Other">Other</option>

              <div class="field">
                     <label class="label"><strong class="has-text-grey">Smoke </strong></label>
                           <div class="control">
                              <div class="select is-primary">
                                 <select name="smoke">
                                 <option value="" >Not Given</option>
                                 <option value="No" >No</option>
                                 <option value="Yes, socially" >Yes, socially</option>
                                 <option value="Yes, regularly" >Yes, regularly</option>
                                 <option value="Rather not say" >Rather not say</option>


Step: 2 SearchController.php

 public function PostSearch(Request $request)

     $filters = [
            'eye_color' => Input::get('eye_color'),
            'drink'    => Input::get('drink'),
      $user = User::where(function ($query) use ($filters) {
            if ($filters['eye_color']) {
                $query->where('eye_color', '=', $filters['eye_color']);
             if ($filters['smoke']) {
                $query->where('smoke', '=', $filters['smoke']);

    return $user;

Here we try to show with two fields, the query filter eye_color  apply on the filter when user select. so here we complete post on Laravel advance search filters with optional fields.

About Author
  • user profile
    Brijpal Sharma, January 29th, 2018

    Hi, I am a web developer, I working on Laravel and CodeIgniter framework, I love to share knowledge and tutorials so I here...

0 Comment

You must be logged in to post a comment.

You may also be interested in...
jQuery plugin to validate the maximum length of the field and count characters.
Deploy Laravel Application To Shared Hosting The Easy and save Way
Laravel advance search filters with optional fields.
Laravel Migrations: Check if Table/Column Already Exists
Connect Multiple databases in the same Laravel project
SEO tool for Laravel Application
Paypal payment gateway integration in Laravel 5.7 step by step
How to Check User online or not in Laravel
How to keep filter data after the click to next button in Laravel pagination
Which is better for web development, Laravel or Flask?