在 Laravel 中附加带有搜索结果的排序路线
Append sort route with search results in Laravel
我想将我的搜索结果 /search?allsearch=
和 url /search?sort=
排序在一起,但我还找不到解决方案。
这是我的代码:
<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>
我的控制器:
$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();
我的路线:
Route::get('/search','ProductController@search')->name('allsearch', 'sort');
独立地,/search?allsearch=
和 /search?sort=
将起作用。但是如果我尝试搜索某些东西,然后对结果进行排序,它会清除搜索请求。
知道如何让它工作吗?
您必须将请求值设置为表单,以便您可以同时使用它们。您可以对每种形式的缺失值使用隐藏输入
搜索表单
<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>
排序形式
<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>
为什么你的路线有两个名字??路线应该有一个唯一的名字
Route::get('search', 'ProductController@search')->name('search');
并在控制器中
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
我想将我的搜索结果 /search?allsearch=
和 url /search?sort=
排序在一起,但我还找不到解决方案。
这是我的代码:
<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>
我的控制器:
$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();
我的路线:
Route::get('/search','ProductController@search')->name('allsearch', 'sort');
独立地,/search?allsearch=
和 /search?sort=
将起作用。但是如果我尝试搜索某些东西,然后对结果进行排序,它会清除搜索请求。
知道如何让它工作吗?
您必须将请求值设置为表单,以便您可以同时使用它们。您可以对每种形式的缺失值使用隐藏输入 搜索表单
<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>
排序形式
<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>
为什么你的路线有两个名字??路线应该有一个唯一的名字
Route::get('search', 'ProductController@search')->name('search');
并在控制器中
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