【问题标题】:Laravel range filter in decimal numbers十进制数的 Laravel 范围过滤器
【发布时间】:2020-11-14 11:15:59
【问题描述】:

我正在尝试使用包含十进制数字(如 1.423)的列过滤产品表。我想过滤介于 1.243 到 1.742 之间的数字。在数据库中,我尝试将列设为浮点、整数和 varchar。但它不起作用。

这是我的控制器:

$products = Product::where('state_id', '=', '1')
                    ->when(!empty($minthick), function ($query) use ($minthick) {
                        return $query->where('thick', '>=', $minthick);
                    })
                    ->when(!empty($maxthick), function ($query) use ($maxthick) {
                        return $query->where('thick', '<=', $maxthick);
                    })
                    ->when(!empty($minwidth), function ($query) use ($minwidth) {
                        return $query->where('width', '>=', $minwidth);
                    })
                    ->when(!empty($maxwidth), function ($query) use ($maxwidth) {
                        return $query->where('width', '<=', $maxwidth);
                    })
                    ->when(!empty($minweight), function ($query) use ($minweight) {
                        return $query->where('weight', '>=', $minweight);
                    })
                    ->when(!empty($maxweight), function ($query) use ($maxweight) {
                        return $query->where('weight', '<=', $maxweight);
                    })
                    ->get();

所有过滤器都应该单独工作。我不明白我在哪里犯错。我有另一个过滤器,其中包含工作正常的类别。这是HTML:

<th scope="col"><!-- Thickness -->
              <label>Thickness:</label>
              <div class="form-row mx-0">
                <div>
                  <select class="form-control" name="minthick">
                    <option value="">Min</option>
                    @forelse ($thick as $th)
                        <option value="{{$th}}">{{ $th }}</option>
                    @empty
                        <p>No filter availvale</p>
                    @endforelse
                  </select>
                </div> -- 
                <div>
                  <select class="form-control" name="maxthick">
                    <option value="">Max</option>
                    @forelse ($thick as $th)
                        <option value="{{$th}}">{{ $th }}</option>
                    @empty
                        <p>No filter availvale</p>
                    @endforelse
                  </select>
                </div>
              </div>
            </th>
            <th scope="col">
              <label>Width</label>
              <div class="form-row mx-0">
                <div class="">
                  <select class="form-control" name="minwidth">
                    <option value="">Min</option>
                    @forelse ($width as $wi)
                        <option value="{{$wi}}">{{ $wi }}</option>
                    @empty
                        <p>No filter availvale</p>
                    @endforelse
                  </select>
                </div> -- 
                <div class="">
                  <select class="form-control" name="maxwidth">
                    <option value="">Max</option>
                    @forelse ($width as $wi)
                        <option value="{{$wi}}">{{ $wi }}</option>
                    @empty
                        <p>No filter availvale</p>
                    @endforelse
                  </select>
                </div>
              </div>
            </th>
          </tr>
          <tr>
            <th scope="col"><!-- Weight -->
              <label>Weight:</label>
              <div class="row form-row">
                <div>
                  <select class="form-control" name="minweight">
                    <option value="">Min</option>
                    @forelse ($weight as $we)
                        <option value="{{$we}}">{{ $we }}</option>
                    @empty
                        <p>No filter availvale</p>
                    @endforelse
                  </select>
                </div> -- 
                <div>
                  <select class="form-control p-0" name="maxweight">
                    <option value="">Max</option>
                    @forelse ($weight as $we)
                        <option value="{{$we}}">{{ $we }}</option>
                    @empty
                        <p>No filter availvale</p>
                    @endforelse
                  </select>
                </div>
              </div>
            </th>

【问题讨论】:

    标签: php html mysql laravel eloquent


    【解决方案1】:

    你有没有尝试过像这样使用 Laravel whereBetween()

    Product::where('state_id', '=', '1')
             ->whereBetween('thick', [$minthick, $maxthick])
             ->whereBetween('width', [$minwidth, $maxwidth])
             ->whereBetween('weight', [$minweight, $maxweight])
             ->get();
    

    【讨论】:

      猜你喜欢
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      相关资源
      最近更新 更多