Laravel - 延伸雄辩,其中条款取决于动态参数 [英] Laravel - extending Eloquent where clauses depending on dynamic parameters
问题描述
我想构造一系列雄辩的WHERE子句,这些子句依赖于我从json对象中收集的搜索参数。
这样的东西(没关系的语法对象,,,这是一个解释,只是为了演示):
$ searchmap =
{
color:red,
height:1,
width:2,
weight:,
size :,
};
然后我拿取对象并解码以获取搜索数组...
$ search = json_decode($ searchmap,true);
如果我的权重和大小设置为null或是一个空字符串,我会有雄辩的代码看起来像这样..
$ gadgets = Gadget :: where('color','=',$ search [颜色'])
- > where('height','=',$ search ['height'])
- >其中('width','=',$ search [' width'])
- > paginate(9);
如果他们有一个值,那么雄辩的代码看起来像这样..
$ gadgets = Gadget :: where('color','=',$ search ['color'])
- > where( 'height','=',$ search ['height'])
- >其中('width','=',$ search ['width'])
- > 'weight','=',$ search ['weight'])
- > where('size','=',$ search ['size'])
- > paginate 9);
有没有办法动态完成这个工作。
我想这个问题应该在这里有一种方式来链接雄辩的动词,基于给定的参数?
在伪上下文中,我正在寻找一些事情像这样
$ gadgets = Gadget ::
foreach($ search as $ key => $ parameter $ {
}
}
- > paginate(9);
可以以某种方式创建where子句的链接类似于此?
感谢您抽出时间查看这个!
更新:
我也想出了这样的东西似乎很好,但如果改进是一个好主意,我想欢迎提出建议。
$ gadgets = New Gadget();
foreach($ search as $ key => $ parameter){
if($ parameter!=''){
$ gadgets = $ gadgets-> where($ key, =',$ parameter);
}
}
$ gadgets = $ gadgets-> paginate(9);
FINAL p>
感谢@lukasgeiter在下面我想我会去这个
$ gadgets = Gadget :: whereNested(function($ query)use($ search){
foreach($ search as $ key => $ value)
{
if($ value! =''){
$ query-> where($ key,'=',$ value);
}
}
},'和');
$ gadgets = $ gadgets-> paginate(9);
很简单。 Laravel的其中
函数允许您传递一组键值对。
$ searchmap = array(
'color'=>'red',
'height'=>'1'
// etc
);
$ gadgets = Gadget :: where($ searchmap) - > paginate(9);
如果你好奇,那就是源的相关部分( \照亮\Database\Query\Builder
)
public function where($ column,$ operator = null,$ value = null,$ boolean ='和')
{
//如果列是数组,我们将假设它是一个键值对数组
//并且可以将它们添加为where子句。我们将保持布尔值我们
//当方法被调用时接收,并将其传递到嵌套的位置。
if(is_array($ column))
{
return $ this-> whereNested(function($ query)use($ column)
{
foreach $ column as $ key => $ value)
{
$ query-> where($ key,'=',$ value);
}
},$布尔值);
}
//更多的代码行...
}
编辑
要更好地控制它(例如将=更改为另一个比较运算符)尝试使用代码laravel直接在内部使用:
$ gadgets = Gadget :: whereNested(function($ query)use($ searchmap)
{
foreach($ searchmap as $ key => $ value)
{
if($ value!=''){
$ query->其中($ key,'=',$ value);
}
}
},'和') - > paginate(9);
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);
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.
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);
Can chaining of where clauses be created in some way similar to this?
Thank you for taking the time to look at this!
UPDATE:
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);
FINAL
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);
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);
If you are curious, that's the relevant part of the source (\Illuminate\Database\Query\Builder
)
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....
}
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 - 延伸雄辩,其中条款取决于动态参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!