php 按价格排序在Mongo Laravel

nhhxz33t  于 2023-04-10  发布在  PHP
关注(0)|答案(2)|浏览(107)

试图根据价格对资产进行分类。

blade.php

<form action="/assets/search/" method="GET" class="form-inline">
@csrf
    <div class="searchByPrice pt-3">
       <p class="font-weight-bold">Search By Price</p>
       <label for="price">Volume</label>
       <input type="range" min="0" max="500000" id="price" step="100" name="price"
        oninput="outputUpdate(value)">
       <output for="price" id="volume">250000</output>
       <script>
           function outputUpdate(vol) {
               document.querySelector('#volume').value = vol;
           }
       </script>
     </div>
</form>

控制器

public function show(Request $request, $_id)
    {
        $query = Asset::query();
        if ($p = $request->price) {
            $query->where('price', '<', $p);
        }
        $assets =  $query->get();
        return view('assets::assets',compact('assets'));
    }

int n();

DD($request->price);
我没有得到想要的结果。有什么解决方案吗?或者在Laravel mongo中按价格排序有什么替代方案吗?

rslzwgfq

rslzwgfq1#

$query = \App\Models\mongo\Log::query();
// Apply sorting
$sort = $request->input('sort', []);
foreach ($sort as $field => $order) {
  $query->orderBy($field, $order);
}

$data = $query->get();
3pmvbmvn

3pmvbmvn2#

我做错的是我需要在if()语句中返回查询。像这样:

public function show(Request $request, $_id)
{
  $query = Asset::query();
  if ($p = $request->price) {
    $query->where('price', '<', $p);
    $assets =  $query->get();
    return view('assets::assets',compact('assets'));
  }  
}

相关问题