Laravel advance search filters with optional fields.

Generic placeholder image
Brijpal Sharma

Date:-March 27th, 2019 , Posted On:- Web Development

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 the author

user profile
Brijpal Sharma

Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...


You must be logged in to post a comment.