dtgj8529 2017-06-01 23:41
浏览 14
已采纳

Laravel不返回子类别结果

I added subcategories to my app with help of here Adding subcategories to laravel 5.4 and before that my search base on category worked just fine now i try to change my search base on subcategory and it doesn't return results.

here is my search function:

 public function search()
{
    $searchTitle = request('title');
    $searchCategory = request('subcategory');
    $searchLocation = request('location');

    $ads = null;

    if($searchTitle || $searchCategory || $searchLocation) {
        $ads = Ad::when($searchTitle, function ($query) use ($searchTitle) {
                return $query->where('title', 'like', "%{$searchTitle}%")
                    ->orWhere('description', 'like', "%{$searchTitle}%");
            })
            ->when($searchCategory, function ($query) use ($searchCategory) {
                return $query->whereHas('subcategory', function ($query) use ($searchCategory) {
                    $query->where('id', $searchCategory);
                });
            })
            ->when($searchLocation, function ($query) use ($searchLocation) {
                return $query->whereHas('location', function ($query) use ($searchLocation) {
                    $query->where('id', $searchLocation);
                });
            })
            ->paginate(10)
            ->appends(request()->query());
    }

    return view('search-result', compact('ads'));
}

this is my search form category part:

<label data-toggle="tooltip" data-placement="top" title="select category" class="sr-only mr-sm-2" for="inlineFormCustomSelect">Category</label>
              <select name="subcategory_id" data-toggle="tooltip" data-placement="top" title="select category"
                      class="custom-select mb-2 mr-sm-2 mb-sm-0" id="inlineFormCustomSelect">
                      <option value="">Select category</option>
                      @foreach ($categories as $category)
                        <optgroup label="{{ $category->name }}">
                          @foreach($category->subcategories as $sub)
                              <option value="{{ $sub->id }}">{{ $sub->name }}</option>
                          @endforeach
                        </optgroup>
                      @endforeach
              </select>

I tried many changes and played with mu function code but nothing still will return no results! error.

  • 写回答

1条回答 默认 最新

  • dongsi4815 2017-06-02 03:42
    关注

    this should probably work

    $ads = Ad::when($searchTitle, function ($query) use ($searchTitle) {
        return $query->where('title', 'like', '%'.$searchTitle.'%')
            ->orWhere('description', 'like', '%'.$searchTitle.'%');
    })
    ->when($searchCategory, function ($query) use ($searchCategory) {
        return $query->whereHas('subcategory', function ($q) use ($searchCategory) {
            $q->where('id', $searchCategory);
        });
    })
    ->when($searchLocation, function ($query) use ($searchLocation) {
        return $query->whereHas('location', function ($q) use ($searchLocation) {
            $q->where('id', $searchLocation);
        });
    })
    ->paginate(10)
    ->appends(request()->query());
    

    also don't forget the names of your inputs .. it should be as corresponding to the request ..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line