0

I need for filtering data based on getting requests,

Current Route

Route::get('datasearch', [Mycontroller::class, 'MyFunction'])->name('this.is.route.name');

Current Forntend form

    <form method="get" enctype="multipart/form-data" action="{{ route('this.is.route.name') }}">
@csrf
        <select class="form-control" name="searchAdmin">
            <option class="hidden" selected disabled>Admin List </option>
            <option value="1">Value 1</option>
            <option value="2">Value 2</option>
        </select>
        <<select class="form-control" name="searchAgent">
            <option class="hidden" selected disabled>Agent List </option>
            <option value="1">Value 1</option>
            <option value="2">Value 2</option>
        </select>
        <input type="submit" value="Search Data" />
    </form>

I need to create below type of URL

http://127.0.0.1:8000/datasearch?filter[dbfieldname1]=searchAdmin&filter[dbfieldname2]=searchAgent
aGreenCoder
  • 158
  • 3
  • 14

3 Answers3

0

Simply provide an array with key values to the route parameters. For example:

route('products.index', ['manufacturer' => 'Samsung','price' => '10000']);
Bhargav Rangani
  • 303
  • 2
  • 12
0

To make request like your requirement, just set form with this content, example in resources\views\home.blade.php:

<form method="get" enctype="multipart/form-data" action="{{ route('search.data') }}">
  <select class="form-control" name="filter[dbfieldname1]">
      <option class="hidden" disabled selected>Admin List </option>
      <option value="1">Value 1</option>
      <option value="2">Value 2</option>
  </select>
  <select class="form-control" name="filter[dbfieldname2]">
      <option class="hidden" disabled selected>Agent List </option>
      <option value="1">Value 1</option>
      <option value="2">Value 2</option>
  </select>
  @csrf
  <input type="submit" value="Search Data" />
</form>

Configure the route routes\web.php:

Route::get('/search/data', [App\Http\Controllers\HomeController::class, 'search'])->name('search.data');

Then set search function as app\Http\Controllers\HomeController.php:

// Get search data
public function search(Request $request)
{
    logger('Function search is working.');
    $filter = $request->filter;
    if($filter) {
        if(isset($filter['dbfieldname1'])){
            logger('Value of dbfieldname1:');
            logger($filter['dbfieldname1']);
        } else {
            logger('Admin list is not choosen.');
        }
        if(isset($filter['dbfieldname2'])){
            logger('Value of dbfieldname2:');
            logger($filter['dbfieldname2']);
        } else {
            logger('Agent list is not choosen.');
        }
    } else {
        logger('Not any lists choosen!');
    }
}

Clear storage\logs\laravel.log, then you will get successful result!

[2022-03-05 05:53:54] local.DEBUG: Function search is working.  
[2022-03-05 05:53:54] local.DEBUG: Admin list is not choosen.  
[2022-03-05 05:53:54] local.DEBUG: Value of dbfieldname2:  
[2022-03-05 05:53:54] local.DEBUG: 2  
[2022-03-05 05:54:24] local.DEBUG: Function search is working.  
[2022-03-05 05:54:24] local.DEBUG: Value of dbfieldname1:  
[2022-03-05 05:54:24] local.DEBUG: 1  
[2022-03-05 05:54:24] local.DEBUG: Agent list is not choosen.  
protoproto
  • 2,081
  • 1
  • 13
  • 13
0

If you're not uploading any files, you probably want to use enctype="application/x-www-form-urlencoded" with your forms. The multipart/form-data is to be used with (special) POST requests for which you need file uploads (I'm not sure what happens when you use it with a GET, it might just get ignored).

Then, you have to keep in mind that every name="..." attribute on your inputs/selects/textareas will determine the key of the data in the URL, i.e.

<input type="text" name="username" value="foobar" />
// example.com?username=foobar
// Laravel will interpret it as ['username' => 'foobar']

You can also change this name to username[] to automatically have the inputs parsed as an array (I believe this all works out of the box with PHP, in the end it depends on your server), i.e.

<input type="text" name="username[]" value="foo" />
<input type="text" name="username[]" value="bar" />
// example.com?username[]=foo&username[]=bar
// Laravel will interpret it as ['username' => ['foo', 'bar']]

Lastly, when you put stuff inside the brackets, it will be interpreted as keys inside of that value like so:

<input type="text" name="username[a]" value="foo" />
<input type="text" name="username[b]" value="bar" />
// example.com?username[a]=foo&username[b]=bar
// Laravel will interpret it as ['username' => ['a' => 'foo', 'b' => 'bar']]
Flame
  • 6,663
  • 3
  • 33
  • 53