laravel中的多个查询搜索

时间:2018-02-07 00:40:39

标签: javascript php ajax laravel search

我尝试在多个输入上获取搜索结果,并且如果用户没有为某些输入设置任何值,则使语句获取或忽略任何输入。

逻辑

用户将:

  1. 选择category
  2. 选择Subcategory
  3. category
  4. Specification中选择brandbothproducts 子类
  5. 到目前为止我所做的是从上面的逻辑中获取所有信息, 我需要的是从该数据中搜索的功能

    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>
    

    截图:

    screen1

    Search function

    很遗憾,我在此数据中没有任何搜索功能,that's why I asked here 需要帮助才能实现

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

    route

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

2 个答案:

答案 0 :(得分:0)

$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();

答案 1 :(得分:0)

试试这个

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();