我正在拼命寻找一种在我的板上制作多个过滤器的方法。目前我正在使用 filter() 函数来过滤数组中的某些元素。我需要用相同的键显示多个数组的元素,但我做不到。
我的 Controller :
$arrays = $arrays->filter(function ($item) use ($five, $four, $three, $two){
return data_get($item, 'note') == $five;
});
我需要用 $five
显示包含音符键的数组$four
$three
和 $two
.是否可以做多个 data_get
在同一个函数中?
最佳答案
您要查找的不一定是 filter()
,而是 groupBy()
函数。
https://laravel.com/docs/7.x/collections#method-groupby
如果您尝试按属性的值进行分组,那么您可能需要做的就是这个
$arrays->groupBy('note');
如果你想按更复杂的东西分组,那么你可以使用回调(类似于你的过滤器)
$arrays->groupBy(function ($item) use ($five, $four, $three, $two) {
// return the value you wish to group by
switch ($item->note) {
case $five:
return 'five';
...
}
});
关于laravel - 在 Laravel 中使用多个过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63227030/