• <bdo id='YcP88'></bdo><ul id='YcP88'></ul>
  1. <tfoot id='YcP88'></tfoot>
    1. <legend id='YcP88'><style id='YcP88'><dir id='YcP88'><q id='YcP88'></q></dir></style></legend>

      <i id='YcP88'><tr id='YcP88'><dt id='YcP88'><q id='YcP88'><span id='YcP88'><b id='YcP88'><form id='YcP88'><ins id='YcP88'></ins><ul id='YcP88'></ul><sub id='YcP88'></sub></form><legend id='YcP88'></legend><bdo id='YcP88'><pre id='YcP88'><center id='YcP88'></center></pre></bdo></b><th id='YcP88'></th></span></q></dt></tr></i><div id='YcP88'><tfoot id='YcP88'></tfoot><dl id='YcP88'><fieldset id='YcP88'></fieldset></dl></div>

      <small id='YcP88'></small><noframes id='YcP88'>

    2. Laravel - 根据动态参数扩展 Eloquent where 子句

      时间:2023-09-24

      <small id='GTjgK'></small><noframes id='GTjgK'>

        • <i id='GTjgK'><tr id='GTjgK'><dt id='GTjgK'><q id='GTjgK'><span id='GTjgK'><b id='GTjgK'><form id='GTjgK'><ins id='GTjgK'></ins><ul id='GTjgK'></ul><sub id='GTjgK'></sub></form><legend id='GTjgK'></legend><bdo id='GTjgK'><pre id='GTjgK'><center id='GTjgK'></center></pre></bdo></b><th id='GTjgK'></th></span></q></dt></tr></i><div id='GTjgK'><tfoot id='GTjgK'></tfoot><dl id='GTjgK'><fieldset id='GTjgK'></fieldset></dl></div>

              <tfoot id='GTjgK'></tfoot>
              • <bdo id='GTjgK'></bdo><ul id='GTjgK'></ul>
                  <tbody id='GTjgK'></tbody>
                <legend id='GTjgK'><style id='GTjgK'><dir id='GTjgK'><q id='GTjgK'></q></dir></style></legend>
                本文介绍了Laravel - 根据动态参数扩展 Eloquent where 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我想根据我从 json 对象收集的搜索参数构建一系列雄辩的 WHERE 子句.

                I would like to construct a series of eloquent WHERE clauses dependent on the search parameters I collect from a json object.

                像这样的东西(不管对象的语法,,,它是一种解释,只是为了演示):

                Something like this (never mind the syntax of object,,, it is an interpretation only to demonstrate):

                $searchmap = "
                {
                    "color": "red",
                    "height": "1",
                    "width": "2",
                    "weight": "",
                    "size": "",
                }";
                

                然后我取对象并解码以获得搜索数组...

                I then take the object and decode to get a search array...

                $search = json_decode($searchmap, true);
                

                如果我的权重和大小设置为 null 或者是一个空字符串",我会有像这样的雄辩代码..

                If my weight and size are set to null or are an 'empty string' I would have eloquent code that looks like this..

                $gadgets = Gadget::where('color',   '=', $search['color'])
                                 ->where('height',  '=', $search['height'])
                                 ->where('width',   '=', $search['width'])
                                 ->paginate(9);
                

                如果它们有一个值,那么雄辩的代码看起来像这样..

                If they have a value then eloquent code would look like this..

                $gadgets = Gadget::where('color',   '=', $search['color'])
                                 ->where('height',  '=', $search['height'])
                                 ->where('width',   '=', $search['width'])
                                 ->where('weight',  '=', $search['weight'])
                                 ->where('size',    '=', $search['size'])
                                 ->paginate(9);
                

                有没有办法动态地实现这一点.

                Is there a way to accomplish this dynamically.

                我想问题应该在于有没有办法根据给定的参数动态链接雄辩的 where 子句?

                I suppose the question should be ins there a way to chain eloquent where clauses dynamically based on a given parameter?

                在伪上下文中,我希望做这样的事情

                In a pseudo context I am looking to do something like this

                $gadgets = Gadget::
                
                    foreach ($search as $key => $parameter) {
                        if ( $parameter <> '' ) {
                            ->where($key, '=', $parameter)
                        }
                    }
                
                ->paginate(9);
                

                是否可以以类似于此的方式创建 where 子句的链接?

                Can chaining of where clauses be created in some way similar to this?

                感谢您花时间看这个!

                更新:

                我也想出了类似这样的东西,似乎效果很好,但如果改进是个好主意,我欢迎提出建议.

                I also came up with something like this that seems to work well but i would like to welcome suggestions if improvement is a good idea.

                $gadgets = New Gadget();
                    foreach ($search as $key => $parameter) {
                        if($parameter != ''){
                            $gadgets = $gadgets->where($key, '=', $parameter);
                        }
                    }
                $gadgets = $gadgets->paginate(9);
                

                <小时>

                最终版

                感谢下面的@lukasgeiter,我想我会选择这个

                And thanks to @lukasgeiter below I think I will go with this

                $gadgets = Gadget::whereNested(function($query) use ($search) {
                    foreach ($search as $key => $value)
                        {
                            if($value != ''){
                                $query->where($key, '=', $value);
                            }
                        }
                }, 'and');
                $gadgets = $gadgets->paginate(9);
                

                推荐答案

                这很简单.Laravel 的 where 函数允许你传入一个键值对数组.

                That's easy. Laravel's where function allows you to pass in an array of key value pairs.

                $searchmap = array(
                    'color' => 'red',
                    'height' => '1'
                    // etc
                );
                
                $gadgets = Gadget::where($searchmap)->paginate(9);
                

                如果你好奇,那是源代码的相关部分 (IlluminateDatabaseQueryBuilder)

                If you are curious, that's the relevant part of the source (IlluminateDatabaseQueryBuilder)

                public function where($column, $operator = null, $value = null, $boolean = 'and')
                {
                    // If the column is an array, we will assume it is an array of key-value pairs
                    // and can add them each as a where clause. We will maintain the boolean we
                    // received when the method was called and pass it into the nested where.
                    if (is_array($column))
                    {
                        return $this->whereNested(function($query) use ($column)
                        {
                            foreach ($column as $key => $value)
                            {
                                $query->where($key, '=', $value);
                            }
                        }, $boolean);
                    }
                
                    // many more lines of code....
                }
                

                编辑

                要对其进行更多控制(例如,将="更改为另一个比较运算符),请尝试直接使用 Laravel 内部使用的代码:

                Edit

                To have more control over it (e.g. changing the "=" to another comparison operator) try using the code laravel uses internally directly:

                $gadgets = Gadget::whereNested(function($query) use ($searchmap)
                        {
                            foreach ($searchmap as $key => $value)
                            {
                                if($value != ''){
                                    $query->where($key, '=', $value);
                                }
                            }
                        }, 'and')->paginate(9);
                

                这篇关于Laravel - 根据动态参数扩展 Eloquent where 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Laravel eloquent 获取所有记录,其中包含多对多关系 下一篇:Laravel - 使用 whereHas 获取最后一行

                相关文章

                最新文章

                <i id='mbb8V'><tr id='mbb8V'><dt id='mbb8V'><q id='mbb8V'><span id='mbb8V'><b id='mbb8V'><form id='mbb8V'><ins id='mbb8V'></ins><ul id='mbb8V'></ul><sub id='mbb8V'></sub></form><legend id='mbb8V'></legend><bdo id='mbb8V'><pre id='mbb8V'><center id='mbb8V'></center></pre></bdo></b><th id='mbb8V'></th></span></q></dt></tr></i><div id='mbb8V'><tfoot id='mbb8V'></tfoot><dl id='mbb8V'><fieldset id='mbb8V'></fieldset></dl></div>

              • <tfoot id='mbb8V'></tfoot>

                1. <legend id='mbb8V'><style id='mbb8V'><dir id='mbb8V'><q id='mbb8V'></q></dir></style></legend>
                    <bdo id='mbb8V'></bdo><ul id='mbb8V'></ul>
                  1. <small id='mbb8V'></small><noframes id='mbb8V'>