繁体   English   中英

Laravel - 从表单的结果更改查询依赖

[英]Laravel - Change query dependend from the results of a form

目前我在 Laravel 中使用 SQL 查询有点困难。

我有一个包含 6 个下拉列表的表单,我通过 get 将其发送到图像控制器。 根据我将发送给控制器的内容,查询应该改变。

如果 a 为“空”(值=“leer”),则应将其从查询中排除。

这是我的 html:

<form action="/filter" method="get">

                                                {{csrf_field()}}
                                                <div class="form-group">
                                                    <label for="brand">Brand</label>
                                                    <select class="form-control" id="brand" name="brand">
                                                        <option value="leer"></option>
                                                        @foreach($brands as $brand)

                                                            <option value="{{$brand->brand}}">{{$brand->brand}}</option>


                                                        @endforeach


                                                    </select>
                                                </div>
                                                <div class="form-group">
                                                    <label for="color">Color</label>
                                                    <select class="form-control" id="color" name="color">
                                                        <option value="leer"></option>

                                                        @foreach($colors as $color)

                                                            <option value="{{$color->color}}">{{$color->color}}</option>


                                                        @endforeach
                                                    </select>
                                                </div>
                                                <div class="form-group">
                                                    <label for="style">Style</label>
                                                    <select class="form-control" id="style" name="style">
                                                        <option value="leer"></option>
                                                        @foreach($styles as $style)

                                                            <option value="{{$style->style}}">{{$style->style}}</option>


                                                        @endforeach
                                                    </select>
                                                </div>
                                                <div class="form-group">
                                                    <label for="material">Material</label>
                                                    <select class="form-control" id="material" name="material">
                                                        <option value="leer"></option>
                                                        @foreach($materials as $material)

                                                            <option value="{{$material->material}}">{{$material->material}}</option>


                                                        @endforeach
                                                    </select>
                                                </div>
                                                <div class="form-group">
                                                    <label for="shape">Shape</label>
                                                    <select class="form-control" id="shape" name="shape">
                                                        <option value="leer"></option>
                                                        @foreach($shapes as $shape)

                                                            <option value="{{$shape->shape}}">{{$shape->shape}}</option>


                                                        @endforeach
                                                    </select>
                                                </div>
                                                <div class="form-group">
                                                    <label for="year">Year</label>
                                                    <select class="form-control" id="year" name="year">
                                                        <option value="leer"></option>
                                                        @foreach($years as $year)

                                                            <option value="{{$year->year}}">{{$year->year}}</option>


                                                        @endforeach

                                                    </select>
                                                </div>

                                                <input type="submit" class="btn" value="Filter">



                                                <button type="button" class="btn" data-dismiss="modal">Close
                                                </button>

                                            </form>

我通过路由发送此表格

Route::get('/filter', 'ImagesController@filter');

这是我的控制器函数,如果在表单中没有选择任何内容,则 where 子句默认应为空。 例如,如果您从品牌下拉列表和颜色下拉列表中选择 somtehing,则查询应如下所示 where([['brand', $brand],['color',$color]])->paginate(12);

    public function filter(Request $request){

    $brand = $request->brand;
    $color = $request->color;
    $style = $request->style;
    $material = $request->material;
    $year = $request->year;
    $shape = $request->shape;

  $images = DB::table('images')->select('brand', 'color', 'style', 'material', 'shape', 'year', 'id', 'path', 'created_at')->where('year',$year)->paginate(12);

    if ($brand == 'leer') {
        $images->where('brand', '=', $brand);
    }

    if ($color == 'leer') {
        $images->where('color', '=', $color);
    }

    if ($style == 'leer') {
        $images->where('style', '=', $style);
    }

    if ($material == 'leer') {
        $images->where('material', '=', $material);
    }

    if ($shape == 'leer') {
        $images->where('shape', '=', $shape);
    }

    if ($year == 'leer') {
        $images->where('year', '=', $year);
    }




    return view('index')->with(compact('images'));

有时它有效,但我认为它是偶然的,显然它不起作用。

我将衷心感谢您的帮助。

非常感谢拉尔斯。

您需要在 where 子句之后调用 paginate。 builder 对象上的分页会生成 LengthAwarePaginator 对象。 对此分页器的 where 调用会导致分页集合上的 where(通过魔术方法 __call),这不是预期的响应。

尝试:

$imagesQuery = DB::table('images')->select('brand', 'color', 'style', 'material', 'shape', 'year', 'id', 'path', 'created_at')->where('year',$year);

        if ($brand == 'leer') {
            $imagesQuery->where('brand', '=', $brand);
        }

        if ($color == 'leer') {
            $imagesQuery->where('color', '=', $color);
        }

        if ($style == 'leer') {
            $imagesQuery->where('style', '=', $style);
        }

        if ($material == 'leer') {
            $imagesQuery->where('material', '=', $material);
        }

        if ($shape == 'leer') {
            $imagesQuery->where('shape', '=', $shape);
        }

        if ($year == 'leer') {
            $imagesQuery->where('year', '=', $year);
        }

$images = $imagesQuery->paginate(12);

return view('index')->with(compact('images'));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM