laravel - 在 Laravel 中使用多个过滤器

标签 laravel

我正在拼命寻找一种在我的板上制作多个过滤器的方法。目前我正在使用 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/

相关文章:

laravel 5如何添加到子模型类中的$with/$appends字段

mysql - 十月 cms - Eloquent - 从 sql 查询创建模型查询的问题

angularjs - 因为我现在正在使用 Laravel,所以我应该学习 Vuejs/Angular/React 吗?

Laravel Spatie 权限 - 用户和角色关系仅获取 id 和名称

laravel - 为什么有 2 个版本的 Laravel?

php - Laravel Eloquent Complex Join Statement - 提供了具体的例子

php - 一般错误 1215 : cannot add foreign key constraint

php - 未定义索引 : Laravel

php - 带 Stripe 的 Laravel 返回 "No such plan: 1 error"

jquery - 使用 laravel 在模态 Bootstrap 中 Bootstrap 警报