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

JOHNMAC's avatar

filter results from dropdown in laravel

I wants to filter the results from dropdown, like when I select an option from dropdown then only its related data show this is my db table

I have written some code in js but nthing happens blade.php:

    <div class="form-group">
              <label class="text-left">Select Highest Record</label>
              <select name="battery" id="battery" onchange="battery()">
                <option value="">Select Highest Record</option>
                @foreach ($pdfdata as $pdfdata)
                <option value="">Select Highest Verbal</option>
                @endforeach
              </select>
            </div>
           

    <table class="table table-hover" id="table_id">
          <tbody><tr>
            <th>File_ID</th>
            <th>Battery</th>
            <th>No_of_questions_attempted</th>
            <th>SAS</th>
            <th>NPR</th>
            <th>ST</th>
            <th>GR</th>
          </tr>
          
        </tbody></table>

controller:

any suggestion to resolve it?

0 likes
32 replies
deansatch's avatar

Start by removing the onchange attribute. And your option values are all empty at the moment

deansatch's avatar

Rename your foreach variables - they are the same at the moment

foreach($pdfdata as $data)

$data->file_id

1 like
munazzil's avatar

Inside foreach you can't use same variable name change it,

    @foreach ($pdfdata as $pdfdata1) //or something
JOHNMAC's avatar

@MUNAZZIL - I have changed it and getting all data now I just wants to filter it according to dropdown options

munazzil's avatar

Then you have to use @if condition and get the data to there Then you have to use every drop down,

 @if($pdfdata->file_id)

    <th>File_ID</th>
    <td>{{ $pdfdata->file_id }}</td>

     //other conditions

 @endif
deansatch's avatar

I think we need to see your current full code. View, controller function for initial page load, & controller function for your $.get()

1 like
deansatch's avatar

Where is the function for the 'importpdfs' route? $.get( 'importpdfs' , { battery : battery }...

deansatch's avatar

Still not seeing it in your routes. There is no uri 'importpdfs'. If you check your console in the browser inspector when filtering I expect you are getting a 404 error?

deansatch's avatar

@LARA1 - ok well firstly it seems you are using jquery but it can't find the library. Are you including it in a script tag at the end of your html doc? If so you will have to move the on change script to the bottom, after your jquery inclusion

1 like
deansatch's avatar

@LARA1 - you mean in the console it says 404? As I said, you don't actually have a route for the url importpdfs so you need to create one, and a controller method to handle it.


            Route::get('importpdfs/{battery}', 'YourController@filter');


    public function filter($battery)
    {
        
        //return your filtered result
    }

JOHNMAC's avatar

@DEANSATCH - what I should add into the controller ,,, I think it will not take more than 3-4min for u to check it now its only giving these two errors

  app.js:49278 CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token

  app.js:37806 [Vue warn]: Cannot find element: #app
JOHNMAC's avatar

@Snapey plz can u tell me the last thing what shout I write in this function to get filtered results (now m getting all results)

  public function filter($battery)
  {
    //
  }
Snapey's avatar

Sorry, didn't think I was involved in this one?

1 like
Snapey's avatar

I dont know where you are with this. You are trying to tackle too many issues at once

deansatch's avatar

I’m trying to help you fix each issue as it comes but you can’t expect us to just write your app for you. At least attempt to write the code yourself and we will help if you get stuck.

So you are doing an Ajax call to your empty method now. According to your Jquery script you need that method to return the html table with filtered results so start there then refactor. You already wrote the query to return the collection unfiltered, so now copy that query and factor in the $battery car to filter it further. Then do your foreach and format it as your table then return it. Keep an eye on your console for js errors and keep using dd() to check you are getting what you want at each step. Use your network tab in the inspector to see your ajax responses.

1 like
JOHNMAC's avatar

@DEANSATCH - m trying to show single values in dropdown but it is not working for me, I hv tried it from documentation: Query Builder and Collections:

  public function filter($battery)
  {
    $pdfdata = DB::table('importodfs')
                    ->limit(4)
                    ->first();
    $array = [
       'Verbal', 'Quantitative', 'Non-Verbal', 'Spatial'
      ];

    $collection = collect($array);

    $unique_data = $collection->unique()->values()->all(); 

    return view('showrecord/array', compact('pdfdata', 'importpdfs', 'array'));
  }

    <div class="form-group">
              <label class="text-left">Select Highest Record</label>
              <select name="battery" id="battery">
                <option value="">Select Highest Record</option>
                @foreach ($pdfdata as $data)
                <option value="{{ $data->file_id }}">Select Highest {{ $data->Battery}}</option>
                @endforeach
              </select>
            </div>
Snapey's avatar

This does not make any sense

$pdfdata = DB::table('importodfs')
                    ->limit(4)
                    ->first();

the first on the end will return a single model

and now your table is called importodfs ???

Its just a mess. Work though it bit by bit, checking the data is what you expect at each stage

1 like
deansatch's avatar

@LARA1 - What is in your importpdfs table? As Snapey said, your current query is only going to return one row so your foreach should only produce a single option field

deansatch's avatar

@LARA1 - Ok, so for example, non-verbal is in the table multiple times and it has a different file_id each time...so why would you want to show non-verbal once in your dropdown menu with its file_id as the value if that value could be 1 or 2 (based on your db screenshot)?

What exactly do you expect your output to look like? Paste your html for the select options with the variables replaced by actual values if it is easier

1 like
JOHNMAC's avatar

@DEANSATCH - Glad that u understand it,,, "so why would you want to show non-verbal once in your dropdown menu with its file_id as the value if that value could be 1 or 2",,,,,,,,,,,,,,,, for this reason I tried to take these verbal, non-verbal, quantitative, spatial in an array to avoid this ""as the value if that value could be 1 or 2""

    $array = [
     'Verbal', 'Quantitative', 'Non-Verbal', 'Spatial'
   ];

"my above code can be wrong,"

current code of html:

         <div class="form-group">
              <label class="text-left">Select Highest Record</label>
              <select name="battery" id="battery">
                <option value="">Select Highest Record</option>
                @foreach ($pdfdata as $data)
                <option value="{{ $data->file_id }}">Select Highest {{ $data->Battery}}</option>
                @endforeach
              </select>
            </div>

         showing output:
      
        Select Highest Verbal
        Select Highest Quantitative
        Select Highest Non-Verbal
        Select Highest Saptial
        Select Highest Verbal
        Select Highest Quantitative
        Select Highest Non-Verbal
        Select Highest Saptial

m expecting it as

            <div class="form-group">
              <label class="text-left">Select Highest Record</label>
              <select name="" id="">
                <option>Select Highest Record</option>
                <option value="">Select Highest Verbal</option>
                <option value="">Select Highest Quantitative</option>
                <option value="">Select Highest Non-Verbal</option>
               <option value="">Select Highest Spatial</option>
              </select>
            </div>
deansatch's avatar

@LARA1 - You haven't filled in the value="" on your expected output - what should the values be?

btw - to get those results without the values using your current code you can just do this:


@foreach ($array as $data)
                <option value="">Select Highest {{ $data}}</option>
@endforeach

But I doubt this is really what you want to do

JOHNMAC's avatar

@DEANSATCH - Undefined variable: array

with both

     <option value="">Select Highest {{ $data}}</option>

     <option value="{{ $data->file_id }}">Select Highest {{ $data}}</option>
Snapey's avatar

You've created an array of options. Just pass it to the view and loop over it.

pretty simple, non-laravel php

deansatch's avatar

@LARA1 - are you still passing the array to the view?


return view('showrecord/array', compact('pdfdata', 'importpdfs', 'array'));

JOHNMAC's avatar

@DEANSATCH - no, I was passing this: return json_encode($array);

@snapey I pass it: return view('showrecord/array', compact('pdfdata', 'importpdfs', 'array')); but same error and if I use Select Highest {{ $data->Battery}}

then it show all the previous one records

deansatch's avatar

@LARA1 - so just pass the array without encoding to the view. But as I say...I'm pretty sure this isn't going to achieve your real end goal. Surely you want some sort of value for each option?

JOHNMAC's avatar

@DEANSATCH - given this value value="{{ $data->file_id }}" but not working,,, still Undefined variable: array

Next

Please or to participate in this conversation.