How to send get parameters as pretty URL on search form in Laravel?

北城余情 提交于 2021-01-05 07:25:26

问题


I have search form to list properties/ads through certain criteria (city, price, quadrature, property type). I am trying to get pretty url like this for example

project/search/city/London/price/1_10000/quadrature/1_150/propertyType/flat 

instead of this like it is now

project/filter?_token=mCwLL58vOxGHtxEBmntPPcks7nV9n3DHXCNKt7hE&city=London&min_price=1&max_price=10000&min_quadrature=1&max_quadrature=150&propertyType=flat&submit=

It is my first time to do something like this, and I am relatively new in Laravel. I tried with javascript's onsubmit function to rewrite the URL but that didn't work, also I googled and tried to create a method for form action then read the query strings and redirect to one with pretty url but I failed. Any help is greatly appreciated. Here is some of my code, but it doesn't work obviously:

web.php

Route::get('/search', 'CategoryController@index');
Route::get('/filter', 'CategoryController@filter');

CategoryController.php

<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CategoryController extends Controller
{
public function index()
    {
    $data = \DB::table('properties');
    return view('categories.search', compact('data'));
    }

    public function filter(Request $request)
    {
        $data = \DB::table('properties');

        if ($request->city) {
        $data = $data->where('city', 'LIKE', "%" . $request->city . "%");
        }

        if ($request->min_price && $request->max_price ) {
            $data = $data->where('price', '>=', $request->min_price)
                     ->where('price', '<=', $request->max_price);
        }

        if ($request->min_quadrature && $request->max_quadrature ) {
            $data = $data->where('quadrature', '>=', $request->min_quadrature)
                     ->where('quadrature', '<=', $request->max_quadrature);
        }

        $data = $data->paginate(10);

        return view('categories.search', compact('data'));
    }

}

search.blade.php

<form id="form" action="/filter" method="GET">
    @csrf

      <div class="row">
        <div class="col-md-5 mb-3">
          <label>City</label>
          <input name="city" list="result" id="input" class="form-control">
          <datalist id="result"></datalist>
        </div>
      </div>

      <div class="row">
        <div class="col-md-6 mb-6">
          <label>Price</label>
          <input type="number" name="min_price" class="form-control" placeholder="Min Price">
          <input type="number" name="max_price" class="form-control" placeholder="Max Price">
        </div>
        <div class="col-md-6 mb-6">
          <label>Quadrature</label>
          <input type="number" name="min_quadrature" class="form-control" placeholder="Min quadrature">
          <input type="number" name="max_quadrature" class="form-control" placeholder="Max quadrature">
        </div>
      </div>

      <hr class="mb-4">

      <div class="row">
        <div class="col-md-5 mb-6">
          <h5>Property type</h4>
            <div class="d-block my-3 ">
              <div class="custom-control custom-radio">
                <input id="house" name="propertyType" value="house" type="radio" class="custom-control-input">
                <label class="custom-control-label" for="house">House</label>
              </div>
              <div class="custom-control custom-radio">
                <input id="flat" name="propertyType" value="flat" type="radio" class="custom-control-input">
                <label class="custom-control-label" for="flat">Flat</label>
              </div>
      </div>

    <hr class="mb-4">

    <button name="submit" class="btn btn-primary btn-lg btn-block" type="submit" onsubmit="window.location.href=this.action + '/' + encodeURIComponent(this.elements['city'].value) + encodeURIComponent(this.elements['min_price'].value) + encodeURIComponent(this.elements['max_price'].value) + encodeURIComponent(this.elements['min_quadrature'].value) + encodeURIComponent(this.elements['max_quadrature'].value) + encodeURIComponent(this.elements['propertyType'].value) + encodeURIComponent(this.elements['propertyBidAsk'].value) + encodeURIComponent(this.elements['propertyPayment'].value); return false;">Search</button>

  </form>

回答1:


You can achieve this by using Laravel's route parameters:

web.php:

Route::get('/search/city/{city}/price/{price}/quadrature/{quadrature}/propertyType/{propertyType}', 'CategoryController@index');

CategoryController.php:

public function index($city, $price, $quadrature, $propertyType)
{
    // Do stuff...
}



回答2:


You can make without javascript...

Html Form:

    <form id="form" action="{{ route('filter') }}" method="GET">
     ... <!-- Your html -->
    <button name="submit" class="btn btn-primary btn-lg btn-block" type="submit" >Search</button>

Route file:

    <?php
    // ... Your Routes
    Route::get('/filter', function(Illuminate\Http\Request $request) {
      // Redirect to named route
      return redirect()->route('search', ['city' => $request['city'], 'price' => $request['price'], 'quadrature' => $request['quadrature'], 'propertyType' => $request['propertyType']]);    
    })->name('filter');
    Route::get('/search/city/{city}/price/{price}/quadrature/{quadrature}/propertyType/{propertyType}', 'CategoryController@filter')->name('search');

Controller file:

    <?php
    // ... Your Class namespace and libraries
    class CategoryController extends Controller
    {
      public function filter(Request $request)
      {
        dd( $request );
        // ... Your code
      }
    }


来源:https://stackoverflow.com/questions/56755611/how-to-send-get-parameters-as-pretty-url-on-search-form-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!