doumianfeng6979 2019-07-19 07:02
浏览 86
已采纳

在Laravel中显示搜索表单结果的问题

I building an application for ads/properties in Laravel. I have a search form with filters that are checkboxes. I am having a problem when I select two options from the same request for example supply, demand that are PropertyBidAsk I get only results from demand and not both, also I would like to keep both checked after form submission. Supply and demand are values in the category column in the categories table. Any help is appreciated. Here is my code.

CategoryController.php:

public function search(Request $request, Property $property)
{
    $category = $property->category;

    $query = Property::query();

    if ($request->has('propertyBidAsk')) {
        $request->get('propertyBidAsk');
    }

    if ($request->propertyBidAsK == 'supply' && $request->propertyBidAsk== 'demand') {
        $query->whereHas('category', function ($query) use ($request) {
            $query->where('category', 'like', '%' . $request->propertyBidAsk . '%');
        });
    } elseif ($request->propertyBidAsk == 'supply') {
        $query->whereHas('category', function ($query) use ($request) {
            $query->where('category', 'like', '%' . 'supply' . '%');
        });
    } else if ($request->propertyBidAsk == 'demand') {
        $query->whereHas('category', function ($query) use ($request) {
            $query->where('category', 'like', '%' . 'demand' . '%');
        });
    }

    $results = $query->paginate(6);

    return view('search', compact('category', 'results', 'request'));
}

search.blade.php:

<div class="col-md-2 mb-6">
    <h5>Payment Method</h4>
    <div class="d-block my-3">
        <div class="custom-control custom-checkbox">
            <input id="supply" name="propertyBidAsk" value="supply" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'supply') checked @endif>
            <label class="custom-control-label" for="supply">supply</label>
        </div>
        <div class="custom-control custom-checkbox">
            <input id="demand" name="propertyBidAsk" value="demand" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'demand') checked @endif>
            <label class="custom-control-label" for="demand">demand</label>
        </div>
    </div>
</div>
  • 写回答

2条回答 默认 最新

  • douchui7332 2019-07-19 07:15
    关注

    To get the multi select output with same name attr, update name of your checkbox input to propertyBidAsk[] as array. You will get input array.

    <input id="supply" name="propertyBidAsk[]" value="supply" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'supply') checked @endif>
    
    
    <input id="demand" name="propertyBidAsk[]" value="demand" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'demand') checked @endif>
    

    In controller will get the value by.

    public function search(Request $request, Property $property)
    {
        $category = $property->category;
    
        $query = Property::query();
    
        if ($request->has('propertyBidAsk')) {
            $request->get('propertyBidAsk');
        }
    
    
        $propertyBidAsk = $request->input('propertyBidAsk'); //$propertyBidAsk will get in array
    
        if (in_array('supply', $propertyBidAsk)  && in_array('demand', $propertyBidAsk)){
            $query->whereHas('category', function ($query) use ($request) {
                $query->where('category', 'like', '%demand%')->orWhere('category', 'like', '%supply%')
            });
        } else if(in_array('supply', $propertyBidAsk)){
            $query->whereHas('category', function ($query) use ($request) {
                $query->where('category', 'like', '%' . 'supply' . '%');
            });
        } else  if(in_array('demand', $propertyBidAsk)){
            $query->whereHas('category', function ($query) use ($request) {
                $query->where('category', 'like', '%' . 'demand' . '%');
            });
        }
    
        $results = $query->paginate(6);
    
        return view('search', compact('category', 'results', 'request'));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里