Laravel Sortby过滤搜索产品选择

时间:2018-01-23 09:28:20

标签: laravel laravel-5

使用process.on('unhandledRejection', function(reason, p){ console.log("Possibly Unhandled Rejection at: Promise ", p, " reason: ", reason); // application specific logging here }); 构建产品搜索过滤器的最佳方式是什么?

我的路线:

<select>

现在我想添加一个带有Route::any ( '/search', function () { $q = Input::get ( 'q' ); if($q != ""){ $products = Product::where ( 'name', 'LIKE', '%' . $q . '%' )->orWhere ( 'description', 'LIKE', '%' . $q . '%' )->paginate (200)->setPath ( '' ); $pagination = $products->appends ( array ( 'q' => Input::get ( 'q' ) ) ); if (count ( $products ) > 0) return view ( 'search' )->withDetails ( $products )->withQuery ( $q ); } return view ( 'search' )->withMessage ( 'No Products!' ); } ); 的下拉过滤器到搜索页面,如:

<select>

我可以在我的路线中这样做:

<select name="sortby">

  <option value="newproducts">New Products</option>

  <option value="cheapprice">Cheapest Price</option>

</select>

感谢您提供一些提示:)

1 个答案:

答案 0 :(得分:0)

当然你可以在那里添加它,但不是有条件的。您已经将查询参数传递给搜索,只需选择一个名称并将其发送。您可以在搜索$ q时使用->orderBy(...)。{/ p>

// from your html
<select name="sortby">

// in your controller/route
$products->orderBy(request->get('sortBy'))

// new products
If (request->get('sortBy') === 'newproducts')
    $products->orderBy('created_at', 'desc');

// cheap price
If (request->get('sortBy') === 'cheapprice')
    $products->orderBy('price', 'asc');

https://laravel.com/docs/5.5/eloquent-collections#available-methods