【问题标题】:Multiple query search in laravellaravel 中的多查询搜索
【发布时间】:2018-02-07 00:40:39
【问题描述】:

我尝试获取多个输入的搜索结果,并在用户没有为某些输入设置任何值的情况下生成语句以获取或忽略任何输入。

逻辑

用户将:

  1. 选择category
  2. category 中选择Subcategory
  3. products中选择Specificationbrandboth 子类

到目前为止,我所做的是从上面的逻辑中获取所有信息,我需要的是从该数据中搜索的功能

代码

Form

<form action="{{route('finderfunction')}}" class="mt-20">
                {{csrf_field()}}
                <div class="col-md-3">
                    <label for="category_id">category</label>
                    <select name="category_id" class="form-control">
                        <option value="">Select Category</option>
                        @foreach($categories as $category)
                            <option value="{{$category->id}}">{{$category->title}}</option>
                        @endforeach
                    </select>
                </div><!-- end col-md-3 -->
                <div class="col-md-3">
                    <label for="subcategory_id">subcategory</label>
                    <select name="subcategory_id" class="form-control">
                        <option value="">Select Subcategory</option>
                    </select>
                </div><!-- end col-md-3 -->
                <div class="col-md-3">
                    <label for="specification_id">specifications</label>
                    <select name="specification_id" class="form-control">
                        <option value="">Select Specification</option>
                    </select>
                </div><!-- end col-md-3 -->
                <div class="col-md-3">
                    <label for="brand_id">brands</label>
                    <select name="brand_id" class="form-control">
                        <option value="">Select Brand</option>
                    </select>
                </div><!-- end col-md-3 -->
                <div class="row">
                    <div class="col-md-3 col-md-offset-9">
                        <button style="margin: 20px;" class="pull-right btn btn-info" type="submit">Find</button>
                    </div>
                </div>
              </form>

JavaScript

<!-- get subcategories -->
<script type="text/javascript">
  $(document).ready(function() {
    $('select[name="category_id"]').on('change', function() {
      var categoryID = $(this).val();
      if(categoryID) {
      $.ajax({
        url: '{{ url('getSubCategories') }}/'+encodeURI(categoryID),
        type: "GET",
        dataType: "json",
        success:function(data) {
        $('select[name="subcategory_id"]').empty().append("<option value='' selected>Select</option>");
        $.each(data, function(key, value) {
            $('select[name="subcategory_id"]').append('<option value="'+ value['id'] +'">'+ value['title'] +'</option>');
            });
        }
      });
      }else{
        $('select[name="subcategory_id"]').empty().append("<option value='' selected>Select</option>");
      }
    });
  });
</script>
<!-- get specifications -->
<script type="text/javascript">
  $(document).ready(function() {
    $('select[name="subcategory_id"]').on('change', function() {
      var subcategoryID = $(this).val();
      if(subcategoryID) {
      $.ajax({
        url: '{{ url('getspecifications') }}/'+encodeURI(subcategoryID),
        type: "GET",
        dataType: "json",
        success:function(data) {
            $('select[name="specification_id"]').empty().append("<option value='' selected>Select</option>");
            $.each(data, function(key, value) {
                  $('select[name="specification_id"]').append(
                        "<option class='form-control' value='"+ value['id'] +"'>"+ value['title'] +"</option>");
            });
        }
      });
      }else{
        $('select[name="specification_id"]').empty().append("<option value='' selected>Select</option>");
      }
    });
  });
</script>
<!-- get brands -->
<script type="text/javascript">
  $(document).ready(function() {
    $('select[name="subcategory_id"]').on('change', function() {
      var subcategoryID = $(this).val();
      if(subcategoryID) {
      $.ajax({
        url: '{{ url('getbrands') }}/'+encodeURI(subcategoryID),
        type: "GET",
        dataType: "json",
        success:function(data) {
        $('select[name="brand_id"]').empty().append("<option value='' selected>Select</option>");
        $.each(data, function(key, value) {
            $('select[name="brand_id"]').append('<option class="form-control" value="'+ value['id'] +'">'+ value['title'] +'</option>');
            });
        }
      });
      }else{
        $('select[name="brand_id"]').empty().append("<option value='' selected>Select</option>");
      }
    });
  });
</script>

截图:

Search function

很遗憾我没有任何搜索功能,that's why I asked here 需要帮助

public function finderfunction(Request $request) {
        //
}

route

Route::any('/finder', 'frontend\SearchController@finderfunction')->name('finderfunction');

【问题讨论】:

  • 您可以使用这对 laracast 中显示的干净解决方案:This onethe consecutive one
  • 我把它放在这里是因为我真的很累 RN 来帮助你编写代码,但是这些视频真的很清楚。 Jeff 使用这些过滤器类的方式很好。

标签: javascript php ajax laravel search


【解决方案1】:

试试这个

return DB::table('products')
        ->where(function($query) use ($request){
          if($request->has('category')){
                        $query->where('category','=',$request->category);
                    }
                    if($request->has('subcategory')){
                        $query->where('subcategory','=',$request->subcategory);
                    }
                 })
                 ->where(function($query) use($request){
                    if($request->has('specifications') && $request->has('brands')){
                        $query->where('specifications','=',$request->specifications)->where('brands','=',$request->brands); // comes specifications and brand
                    }
                    if($request->has('specifications')){
                        $query->orWhere('specifications','=',$request->specifications);
                    }
                    if($request->has('brands')){
                        $query->orWhere('brands','=',$request->brands);
                    }
                 })->get();

【讨论】:

  • return 一开始?你确定吗?
  • 未定义变量:查询
  • 您的function($query)-&gt;where(function() use($request){ 之前关闭,如何定义其他query
  • 在我看来它正在工作,但等到我完全检查它。
  • 我在这里有问题,关于 specifications 的问题是针对子类别和品牌的,我在产品表中获得了他们的 id,因为只使用 1 个 id 就是这样,但对于规格,我使用的是名为 3rd 的表product_subspecification 现在,当我使用您的方法时,它会返回基于subcategorybrands 的数据库,但不会返回基于specifications 的数据库,因为它正在查看产品表,而它必须查看我的product_subspecification 表。在下一条评论中,我将分享我的查询,我是如何首先获得这些数据的。
【解决方案2】:
$post = $request->all(); //Initialize the request
$test = DB::table('products')
->where('subcategory_id','=',$post['subcategory_id']);

if(isset($post['specification_id']) && !(isset($post['brand_id'])))
{
$test= $test->where('subspecification_id','=',$post['specification_id']);
}
elseif(!(isset($post['specification_id'])) && isset($post['brand_id']))
{
$test= $test->where('brand_id','=',$post['brand_id']);
}
else
{
$test= $test->where('subspecification_id','=',$post['specification_id'])
->where('brand_id','=',$post['brand_id']);
}

$test = $test->get();

【讨论】:

  • 不知道为什么,但是你的第一个答案返回了一些数据,这个没有!
  • 我很糟糕,我错过了一些终止。我再次编辑了它:D
  • 未定义变量:查询
  • 您是否像我在 post 变量声明下方的第三行中那样声明了查询?
  • 你是这个意思吗? $query = 如果是,那么是的,我做到了
猜你喜欢
  • 2018-05-16
  • 2014-10-25
  • 1970-01-01
  • 2013-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-04
  • 1970-01-01
相关资源
最近更新 更多