hushhush
hushhush

Reputation: 157

Append sort route with search results in Laravel

I want to append my search result /search?allsearch= and sort url /search?sort= together but I can't find the solution yet. Here is my code:

<form class="form-inline my-2 my-lg-0 rounded left-addon-search inner-addon" action="/search" method="GET">
    <i class="fas fa-search"></i>
    <input type="text" name="allsearch" class="search-field rounded" placeholder="Search" aria-label="Search" aria-describedby="search-addon" />
</form>

<select id="sort" name="sort">
    <option value="">Relevance</option>
    <option value="product_price_low_high" @if(isset($_GET['sort']) && $_GET['sort']=="product_price_low_high") selected="" @endif>Price: Low to High</option>
    <option value="product_price_high_low" @if(isset($_GET['sort']) && $_GET['sort']=="product_price_high_low") selected="" @endif>Price: High to Low</option>
    <option value="product_latest" @if(isset($_GET['sort']) && $_GET['sort']=="product_latest") selected="" @endif>Latest Arrivals</option>
</select>

My Controller:

$search = $request->allsearch;

$product = DB::table('brands')
    ->join('products', 'brands.id', '=', 'products.brandid')
    ->where('productname','like','%'.$search.'%')
    ->OrWhere('name','like','%'.$search.'%');

// sorting product
if (isset($_GET['sort']) && !empty($_GET['sort'])) {
    if ($_GET['sort'] == "product_price_low_high") {
        $product->orderBy('productprice', 'asc');
    } elseif ($_GET['sort'] == "product_price_high_low") {
        $product->orderBy('productprice', 'desc');
    } elseif($_GET['sort'] == "product_latest") {
        $product->orderBy('id', 'desc');
    }
}

$product = $product->get();

My route:

Route::get('/search','ProductController@search')->name('allsearch', 'sort');

Independently, the /search?allsearch= and the /search?sort= will work. But if I try to search for something, then sort the results, it clears out the search request. Any idea how to make this works?

Upvotes: 0

Views: 553

Answers (1)

zahid hasan emon
zahid hasan emon

Reputation: 6233

you have to set the request value to the form so that you can work with both of them. you can use hidden input for the missing value in each form the search form

<form class="form-inline my-2 my-lg-0 rounded left-addon-search inner-addon" action="{{ route('search') }}" method="GET">
    <i class="fas fa-search"></i>
    <input type="text" name="allsearch" value="{{ request()->allsearch }}" class="search-field rounded" placeholder="Search" aria-label="Search" aria-describedby="search-addon" />
    <input type="hidden" name="sort" value="{{ request()->sort }}" />
</form>

the sort form

<form class="form-inline my-2 my-lg-0 rounded left-addon-search inner-addon" action="{{ route('search') }}" method="GET">
    <select id="sort" name="sort">
        <option value="">Relevance</option>
        <option value="product_price_low_high" @if (request()->sort == "product_price_low_high") selected @endif>Price: Low to High</option>
        <option value="product_price_high_low" @if (request()->sort == "product_price_high_low") selected @endif>Price: High to Low</option>
        <option value="product_latest" @if (request()->sort == "product_latest") selected @endif>Latest Arrivals</option>
    </select>
    <input type="hidden" name="allsearch" value="{{ request()->allsearch }}" />
</form> 

why your route has two names?? route shuold have a single unique name

Route::get('search', 'ProductController@search')->name('search');

and in controller

use Illuminate\Http\Request;
public function search(Request $request)
{
    $search = $request->allsearch;

    $product = DB::table('brands')
        ->join('products', 'brands.id', '=', 'products.brandid')
        ->where('productname', 'like', '%'.$search.'%')
        ->orWhere('name', 'like', '%'.$search.'%');

    if ($request->sort == "product_price_low_high") {
        $product->orderBy('productprice', 'asc');
    } elseif ($request->sort == "product_price_high_low") {
        $product->orderBy('productprice', 'desc');
    } elseif ($request->sort == "product_latest") {
        $product->orderBy('id', 'desc');
    }

    $product = $product->get();
    return response/view
}

you are using laravel, so try to code as the laravel way

Upvotes: 1

Related Questions