Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

How to perform Multiple Searches in Laravel?

Guys I already asked about multiple searches in laravel. I have tried with some coding. Let me paste those and kindly help how to move further .

First i will show my screen shot then i will explain my doubt

https://imgur.com/a/pIJm4ZZ

My table name is sales_details with fields(sales_id, date, time, customer_id, customer_name, customer_type, customer_area, customer_booth, no_of_litre, rate_per_litre, total, note).

This is my controller for view

public function index()
    {
        $sales = Sales_details::orderBy('created_at','desc')->paginate(5);

        return view('Sales_details.view')->withsales($sales);
    }

This is my view. Blade

@extends('main')

@section('title','| All data Sales')

@section('content')

<div class="row">
        <div class="col-md-10">


        </div>
     <div class="col-md-2">
            <a href="{{ route('Sales_details.create') }}" class="btn btn-lg btn-block btn-primary btn-h1-spacing"> Sales Entry</a>
     </div>
<hr>
</div><!-- end of row-->

<div class="row">
<div class="col-md-12">

        <caption><h2 align="center">SALES DETAILS PREVIEW</h2></caption>

    <table  id="sales_details" class="table table-bordered">
        <thead>

                    <th > {{ Form::text('search_sid',null,array('class'=>'form-     control','id'=>'search_sid','name'=>'search_sid')) }}</th>

            <th> {{ Form::text('search_date',null,array('class'=>'form-control','id'=>'search_date','name'=>'search_date')) }}</th>

            <th> {{ Form::text('search_time',null,array('class'=>'form-control','id'=>'search_time','name'=>'search_time')) }}</th>

            <th> {{ Form::text('search_id',null,array('class'=>'form-control','id'=>'search_id','name'=>'search_id')) }}</th>

            <th> {{ Form::text('search_type',null,array('class'=>'form-control','id'=>'search_type','name'=>'search_type')) }}</th>

            <th> {{ Form::text('search_name',null,array('class'=>'form-control','id'=>'search_name','name'=>'search_name')) }}</th>

            <th> {{ Form::text('search_area',null,array('class'=>'form-control','id'=>'search_area','name'=>'search_area')) }}</th>

            <th> {{ Form::text('search_booth',null,array('class'=>'form-control','id'=>'search_booth','name'=>'search_booth')) }}</th>

            <th> {{ Form::text('search_rate_per_lit',null,array('class'=>'form-control','id'=>'search_rate_per_lit','name'=>'search_rate_per_lit')) }}</th>

            <th> {{ Form::text('search_no_of_lit',null,array('class'=>'form-control','id'=>'search_no_of_lit','name'=>'search_no_of_lit')) }}</th>

            <th> {{ Form::text('search_total',null,array('class'=>'form-control','id'=>'search_total','name'=>'search_total')) }}</th>

            <th> {{ Form::text('search_note',null,array('class'=>'form-control','id'=>'search_note','name'=>'search_note')) }}</th>

    </thead>

      <thead>

              <th >Sales Id</th>

              <th>Date</th>

              <th>Time</th>

              <th>Customer Id</th>

              <th>Customer Type</th>

              <th>Customer Name</th>

              <th>Customer Area</th>

              <th>Customer Booth</th>

              <th>Rate/Litre</th>

              <th>No of Litres</th>

              <th>Total</th>

              <th>Note</th>

              <th>Action</th>

      </thead>

      <tbody >
            @foreach($sales as $sal)
                <tr>
            
     <td>{{ $sal->sales_id }}</td>

                    <td>{{ $sal->date }}</td>

                    <td>{{ $sal->time }}</td>

                    <td>{{ $sal->customer_id }}</td>

                    <td>{{ $sal->customer_type }}</td>

                    <td>{{ $sal->customer_name }}</td>

                    <td>{{ $sal->customer_area }}</td>

                    <td>{{ $sal->customer_booth }}</td>

                    <td>{{ $sal->rate_per_litre }}</td>

                    <td>{{ $sal->no_of_litre }}</td>

                    <td>{{ $sal->total }}</td>

                    <td>{{ $sal->note }}</td>

    <td>
        <a href="{{ route('Sales_details.show',$sal->id) }}" class="btn btn-success btn-sm" style="width: 100%;"</td>View</a>
 </tr>
            @endforeach
      </tbody>
      </table>

            <div class="text-center">

                {!! $sales->Links(); !!}
            </div>


</div>
</div>
<script type="text/javascript">

    $('#search_name').on('keyup',function(){

    $value=$(this).val();

    $.ajax({

    type : 'get',

    url : '{{URL::to('search_name')}}',

    data:{'search_name':$value},

    success:function(data){
        if(data)
                                {
                                    var result=data;
                                    console.log(result);
                                    

                            } 
    }

    });

    })

    </script>

@endsection

In the view. Blade I have displayed the data in tabular format. And I also have empty text box in each field in the view. Blade files for search purpose. My objective is if I search in name text box it must filter from DB and it should return the values to view. Blade file.

After searching name it displays 2 or 3 rows based on my search say for example I am searching for customer name “Abdul” then I returns 4 rows with different id, date, time,area,booth.

Next I need to search based on date in the returned 4 rows. Say for example if I give a specific date it must be further filtered from the returned 4 rows. The it will return 2 rows then I need to further sort.

Here is my JavaScript code written in the view. Blade page

<script type="text/javascript">

    $('#search_name').on('keyup',function(){

    $value=$(this).val();

    $.ajax({

    type : 'get',

    url : '{{URL::to('search_name')}}',

    data:{'search_name':$value},

    success:function(data){
        if(data)
                                {
                                    var result=data;
                                    console.log(result);
                                    
                            } 
    }

    });

    })

    </script>

And here is my search_name function in controller


 public function search_name(Request $request)

    {
        if($request->ajax())

    {               

                    $sales=Sales_details::where('customer_name','LIKE','%'.$request->search_name.'%')->first();

    return Response($sales);
   }

   }

Now everything is ok. But after returning the return $sales with sorted item. It comes to the view. Blade. But I don’t know how to receive the $sales in view. Blade and display in the < tbody> in for loop in jquery.

Another doubt is if it is possible to display the sorted items in the then my next problem is after that how can I sort further with date or time or id and perform the same task.

Simple to say multiple search option in the view form with multiple 'and' conditions must be searched in live by the user

Anyone Please help me to complete this searching function

0 likes
4 replies
mushood's avatar
mushood
Best Answer
Level 41

Thank you for explaining and being as detailed as possible.

Here are a few tips to guide you to your answer:

  1. Have a single search method on your server with different filters. The filters correspond to the fields by which you want to search

Example:

public function search(Request $request)
{
        $filters = $request->filters;
    $sales = Sales_details::orderBy('created_at','desc');
        foreach($filters as $filter){
        if($filter != null){
            $sales = $sales->where('created_at','desc');
        }
    }
    $sales = $sales->paginate($filter->key, $filter->value);
}

This way allows you to have a single place to manage your filters so you dont end up with a lot of different methods.

  1. If your ajax, you can just track the search fields and send them ALL each time. Thats why you check if your filter is null before applying it.
 data:{
    'search_name':$value,
    'filter_key':$filter_value
},
  1. Bad advice When i used jQuery, I used to construct the HTML on the server and send the HTML in my response. In my JS, i would just set the HTML.

  2. Better advice I now use Vue.js and this is made a lot easier. Check out this course to get started: https://www.laracasts.com/series/learn-vue-2-step-by-step Learning a front end framework will definitely improve your GAME!

1 like
AbdulBazith's avatar

@mushood Thank you .. but i can't understand the coding which is given first.. can you explain or can you send me an appropriate link to make my work easier. vue i don't need now please

1 like
pardeepkumar's avatar

Let understand by below example and try to implement it .

// Dog model
public function scopeSearchBreed($query, $breed)
{
  $query->whereHas('breed', function ($q) use ($breed) {
    $q->where('name', 'like', "%{$breed}%");
  });
}

public function scopeWithinRadius($query, $radius)
{
  $query->where(...); // do math here
}
Then all you need is this:

public function index()
{
  $q = Dog::query();

  if (Input::has('search'))
  {
     // simple where here or another scope, whatever you like
     $q->where('name','like',Input::get('search'));
  }

  if (Input::has('search-breed'))
  {
     $q->searchBreed(Input::get('search-breed'));
  }

  if (Input::has('sex'))
  {
     $q->where('sex', Input::get('sex'));
  }

  if (Input::has('radius'))
  {
     $q->withinRadius(Input::get('radius'));
  }

  $dogs = $q->orderBy(..)->paginate(5);

  // ...
1 like
AbdulBazith's avatar

@pardeepkumar thank you, but what about java script and view page?

i need to pass the values to controller through ajax. I need to pass mutiple values some time that is,

name="Abdul" city="India" date="5-5-2018"

these three are typed by user during the view page that is when all data is viewed the user types the required filter through the text box . All the three values are passed through ajax to the search function the it returns appropriate result

i think you understood my question?

Please or to participate in this conversation.