繁体   English   中英

Laravel集合中的排序组数组

[英]Laravel sort group array in collection

我在数据库查询后得到一个集合。 然后,我通过传递闭包回调对其进行分组。 最后,我想使用传递闭包回调对数组进行排序的每个数组对这些分组数组进行排序。 但是不起作用。 也许它是受保护的 ,如何实现呢?

我在PostController上的代码

$post->comments = $post->comments->groupBy(function($comment){
    return $comment->parent_id == null ? "-1": $comment->parent_id;
})->each(function($comment){
    // TODO: sort array
    uasort($comment, function($a, $b){
        return $a->created_at->lte($b->created_at)? 1: -1;
    });
    return $comment;
});
    Debugbar::info($post->comments);

$ post->评论的信息

Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
        (
            [-1] => Array
                (
                    [0] => Comment Object
                        (
                            [table:protected] => comments
                            [fillable:protected] => Array
                                (
                                    [0] => id
                                )

                            [connection:protected] => 
                            [primaryKey:protected] => id
                            [perPage:protected] => 15
                            [incrementing] => 1
                            [timestamps] => 1
                            [attributes:protected] => Array
                                (
                                    [id] => 1
                                    [user_id] => 4
                                    [post_id] => 1
                                    [parent_id] => 
                                    [comment] => Eligendi ipsum saepe qui velit laudantium.
                                    [created_at] => 2013-11-06 13:28:49
                                    [updated_at] => 2013-11-06 13:28:49
                                )

                            [original:protected] => Array
                                (
                                    [id] => 1
                                    [user_id] => 4
                                    [post_id] => 1
                                    [parent_id] => 
                                    [comment] => Eligendi ipsum saepe qui velit laudantium.
                                    [created_at] => 2013-11-06 13:28:49
                                    [updated_at] => 2013-11-06 13:28:49
                                )

                            [relations:protected] => Array
                                (
                                )

                            [hidden:protected] => Array
                                (
                                )

                            [visible:protected] => Array
                                (
                                )

                            [appends:protected] => Array
                                (
                                )

                            [guarded:protected] => Array
                                (
                                    [0] => *
                                )

                            [dates:protected] => Array
                                (
                                )

                            [touches:protected] => Array
                                (
                                )

                            [observables:protected] => Array
                                (
                                )

                            [with:protected] => Array
                                (
                                )

                            [exists] => 1
                            [softDelete:protected] => 
                        )

                    [1] => Comment Object
                        (
                            [table:protected] => comments
                            [fillable:protected] => Array
                                (
                                    [0] => id
                                )

                            [connection:protected] => 
                            [primaryKey:protected] => id
                            [perPage:protected] => 15
                            [incrementing] => 1
                            [timestamps] => 1
                            [attributes:protected] => Array
                                (
                                    [id] => 2
                                    [user_id] => 63
                                    [post_id] => 1
                                    [parent_id] => 
                                    [comment] => Aperiam ut molestiae.
                                    [created_at] => 2013-08-18 00:15:24
                                    [updated_at] => 2013-08-18 00:15:24
                                )

                            [original:protected] => Array
                                (
                                    [id] => 2
                                    [user_id] => 63
                                    [post_id] => 1
                                    [parent_id] => 
                                    [comment] => Aperiam ut molestiae.
                                    [created_at] => 2013-08-18 00:15:24
                                    [updated_at] => 2013-08-18 00:15:24
                                )

                            [relations:protected] => Array
                                (
                                )

                            [hidden:protected] => Array
                                (
                                )

                            [visible:protected] => Array
                                (
                                )

                            [appends:protected] => Array
                                (
                                )

                            [guarded:protected] => Array
                                (
                                    [0] => *
                                )

                            [dates:protected] => Array
                                (
                                )

                            [touches:protected] => Array
                                (
                                )

                            [observables:protected] => Array
                                (
                                )

                            [with:protected] => Array
                                (
                                )

                            [exists] => 1
                            [softDelete:protected] => 
                        )

                    [2] => Comment Object
                        (
                            [table:protected] => comments
                            [fillable:protected] => Array
                                (
                                    [0] => id
                                )

                            [connection:protected] => 
                            [primaryKey:protected] => id
                            [perPage:protected] => 15
                            [incrementing] => 1
                            [timestamps] => 1
                            [attributes:protected] => Array
                                (
                                    [id] => 3
                                    [user_id] => 4
                                    [post_id] => 1
                                    [parent_id] => 
                                    [comment] => Doloribus ipsa velit unde veritatis qui.
                                    [created_at] => 2013-09-20 05:19:45
                                    [updated_at] => 2013-09-20 05:19:45
                                )

                            [original:protected] => Array
                                (
                                    [id] => 3
                                    [user_id] => 4
                                    [post_id] => 1
                                    [parent_id] => 
                                    [comment] => Doloribus ipsa velit unde veritatis qui.
                                    [created_at] => 2013-09-20 05:19:45
                                    [updated_at] => 2013-09-20 05:19:45
                                )

                            [relations:protected] => Array
                                (
                                )

                            [hidden:protected] => Array
                                (
                                )

                            [visible:protected] => Array
                                (
                                )

                            [appends:protected] => Array
                                (
                                )

                            [guarded:protected] => Array
                                (
                                    [0] => *
                                )

                            [dates:protected] => Array
                                (
                                )

                            [touches:protected] => Array
                                (
                                )

                            [observables:protected] => Array
                                (
                                )

                            [with:protected] => Array
                                (
                                )

                            [exists] => 1
                            [softDelete:protected] => 
                        )

                    [19] => Comment Object
                        (
                            [table:protected] => comments
                            [fillable:protected] => Array
                                (
                                    [0] => id
                                )

                            [connection:protected] => 
                            [primaryKey:protected] => id
                            [perPage:protected] => 15
                            [incrementing] => 1
                            [timestamps] => 1
                            [attributes:protected] => Array
                                (
                                    [id] => 27
                                    [user_id] => 100
                                    [post_id] => 1
                                    [parent_id] => 
                                    [comment] => dsdfsdfdg
                                    [created_at] => 2014-02-17 09:06:08
                                    [updated_at] => 2014-02-17 09:06:08
                                )

                            [original:protected] => Array
                                (
                                    [id] => 27
                                    [user_id] => 100
                                    [post_id] => 1
                                    [parent_id] => 
                                    [comment] => dsdfsdfdg
                                    [created_at] => 2014-02-17 09:06:08
                                    [updated_at] => 2014-02-17 09:06:08
                                )

                            [relations:protected] => Array
                                (
                                )

                            [hidden:protected] => Array
                                (
                                )

                            [visible:protected] => Array
                                (
                                )

                            [appends:protected] => Array
                                (
                                )

                            [guarded:protected] => Array
                                (
                                    [0] => *
                                )

                            [dates:protected] => Array
                                (
                                )

                            [touches:protected] => Array
                                (
                                )

                            [observables:protected] => Array
                                (
                                )

                            [with:protected] => Array
                                (
                                )

                            [exists] => 1
                            [softDelete:protected] => 
                        )

                    [20] => Comment Object
                        (
                            [table:protected] => comments
                            [fillable:protected] => Array
                                (
                                    [0] => id
                                )

                            [connection:protected] => 
                            [primaryKey:protected] => id
                            [perPage:protected] => 15
                            [incrementing] => 1
                            [timestamps] => 1
                            [attributes:protected] => Array
                                (
                                    [id] => 28
                                    [user_id] => 100
                                    [post_id] => 1
                                    [parent_id] => 
                                    [comment] => dsdfsdfdg
                                    [created_at] => 2014-02-17 09:08:14
                                    [updated_at] => 2014-02-17 09:08:14
                                )

                            [original:protected] => Array
                                (
                                    [id] => 28
                                    [user_id] => 100
                                    [post_id] => 1
                                    [parent_id] => 
                                    [comment] => dsdfsdfdg
                                    [created_at] => 2014-02-17 09:08:14
                                    [updated_at] => 2014-02-17 09:08:14
                                )

                            [relations:protected] => Array
                                (
                                )

                            [hidden:protected] => Array
                                (
                                )

                            [visible:protected] => Array
                                (
                                )

                            [appends:protected] => Array
                                (
                                )

                            [guarded:protected] => Array
                                (
                                    [0] => *
                                )

                            [dates:protected] => Array
                                (
                                )

                            [touches:protected] => Array
                                (
                                )

                            [observables:protected] => Array
                                (
                                )

                            [with:protected] => Array
                                (
                                )

                            [exists] => 1
                            [softDelete:protected] => 
                        )

                )

            [5] => Array
                (
                    [0] => Comment Object
                        (
                            [table:protected] => comments
                            [fillable:protected] => Array
                                (
                                    [0] => id
                                )

                            [connection:protected] => 
                            [primaryKey:protected] => id
                            [perPage:protected] => 15
                            [incrementing] => 1
                            [timestamps] => 1
                            [attributes:protected] => Array
                                (
                                    [id] => 7
                                    [user_id] => 12
                                    [post_id] => 1
                                    [parent_id] => 5
                                    [comment] => Consequatur aut consequuntur ut voluptatem et.
                                    [created_at] => 2013-09-19 11:21:35
                                    [updated_at] => 2013-09-19 11:21:35
                                )

                            [original:protected] => Array
                                (
                                    [id] => 7
                                    [user_id] => 12
                                    [post_id] => 1
                                    [parent_id] => 5
                                    [comment] => Consequatur aut consequuntur ut voluptatem et.
                                    [created_at] => 2013-09-19 11:21:35
                                    [updated_at] => 2013-09-19 11:21:35
                                )

                            [relations:protected] => Array
                                (
                                )

                            [hidden:protected] => Array
                                (
                                )

                            [visible:protected] => Array
                                (
                                )

                            [appends:protected] => Array
                                (
                                )

                            [guarded:protected] => Array
                                (
                                    [0] => *
                                )

                            [dates:protected] => Array
                                (
                                )

                            [touches:protected] => Array
                                (
                                )

                            [observables:protected] => Array
                                (
                                )

                            [with:protected] => Array
                                (
                                )

                            [exists] => 1
                            [softDelete:protected] => 
                        )

                )

               )

            [2] => Array
                (
                    [0] => Comment Object
                        (
                            [table:protected] => comments
                            [fillable:protected] => Array
                                (
                                    [0] => id
                                )

                            [connection:protected] => 
                            [primaryKey:protected] => id
                            [perPage:protected] => 15
                            [incrementing] => 1
                            [timestamps] => 1
                            [attributes:protected] => Array
                                (
                                    [id] => 9
                                    [user_id] => 74
                                    [post_id] => 1
                                    [parent_id] => 2
                                    [comment] => Omnis est tenetur eum quo totam.
                                    [created_at] => 2013-11-12 12:40:41
                                    [updated_at] => 2013-11-12 12:40:41
                                )

                            [original:protected] => Array
                                (
                                    [id] => 9
                                    [user_id] => 74
                                    [post_id] => 1
                                    [parent_id] => 2
                                    [comment] => Omnis est tenetur eum quo totam.
                                    [created_at] => 2013-11-12 12:40:41
                                    [updated_at] => 2013-11-12 12:40:41
                                )

                            [relations:protected] => Array
                                (
                                )

                            [hidden:protected] => Array
                                (
                                )

                            [visible:protected] => Array
                                (
                                )

                            [appends:protected] => Array
                                (
                                )

                            [guarded:protected] => Array
                                (
                                    [0] => *
                                )

                            [dates:protected] => Array
                                (
                                )

                            [touches:protected] => Array
                                (
                                )

                            [observables:protected] => Array
                                (
                                )

                            [with:protected] => Array
                                (
                                )

                            [exists] => 1
                            [softDelete:protected] => 
                        )

                )

            [1] => Array
                (
                    [0] => Comment Object
                        (
                            [table:protected] => comments
                            [fillable:protected] => Array
                                (
                                    [0] => id
                                )

                            [connection:protected] => 
                            [primaryKey:protected] => id
                            [perPage:protected] => 15
                            [incrementing] => 1
                            [timestamps] => 1
                            [attributes:protected] => Array
                                (
                                    [id] => 10
                                    [user_id] => 66
                                    [post_id] => 1
                                    [parent_id] => 1
                                    [comment] => Non consequatur excepturi molestias tempore voluptatem.
                                    [created_at] => 2013-09-19 09:13:02
                                    [updated_at] => 2013-09-19 09:13:02
                                )

                            [original:protected] => Array
                                (
                                    [id] => 10
                                    [user_id] => 66
                                    [post_id] => 1
                                    [parent_id] => 1
                                    [comment] => Non consequatur excepturi molestias tempore voluptatem.
                                    [created_at] => 2013-09-19 09:13:02
                                    [updated_at] => 2013-09-19 09:13:02
                                )

                            [relations:protected] => Array
                                (
                                )

                            [hidden:protected] => Array
                                (
                                )

                            [visible:protected] => Array
                                (
                                )

                            [appends:protected] => Array
                                (
                                )

                            [guarded:protected] => Array
                                (
                                    [0] => *
                                )

                            [dates:protected] => Array
                                (
                                )

                            [touches:protected] => Array
                                (
                                )

                            [observables:protected] => Array
                                (
                                )

                            [with:protected] => Array
                                (
                                )

                            [exists] => 1
                            [softDelete:protected] => 
                        )

                )

        )

)

您可以看到“ -1”组按创建日期未排序。

到目前为止,我通过在控制器中创建私有功能来处理分组的注释收集对象来实现此目的。

这是什么样的功能:

private function commentToArrayAndSort(Illuminate\Database\Eloquent\Collection $groupedComments, Closure $sortClosure, Closure $childrenSortClosure = null)
{
    $formattedComments = array();
    $keys = array_keys($groupedComments->toArray());
    for($i = 0; $i < count($keys); $i++)
    {
        $comments = $groupedComments[$keys[$i]];
        $formattedComments[$keys[$i]] = array();
        $counter = 0;
        foreach($comments as $comment)
        {
            $commentArr = $comment->toArray();
            $commentObj = new Comment(array(
                "id" => $commentArr['id'],
                "user_id" => $commentArr['user_id'],
                "post_id" => $commentArr['post_id'],
                "parent_id" => $commentArr['parent_id'],
                "comment" => $commentArr['comment'],
                "created_at" => $commentArr['created_at'],
                "updated_at" => $commentArr['updated_at'],
            ));

            $commentObj->setRelations(array('author'=>new User($comment->toArray()['author'])));
            array_push($formattedComments[$keys[$i]], $commentObj);
            $counter++;
        }

        // Sort Array
        if ($counter > 1) {
            uasort($formattedComments[$keys[$i]], $sortClosure);
        }
    }

    return $formattedComments;
}

然后这样称呼它:

    $post->comments = $this->commentToArrayAndSort($groupedComments, function($a, $b){
        return $a->created_at->gte($b->created_at) ? -1 : 1;
    });

我不知道是否有更好的解决方案。 到目前为止,我明白了。 如果有人可以改善我的答案或有更好的解决方案,请发布并分享给我帮助

谢谢 :)

我正在搜索一些相关的东西:在将数据传递到视图之前,必须在控制器中从Eloquent操纵数据。 因此,例如:

$events = App\Models\Event::->get();
$eventsDecade = [];
    foreach ($events as $key => $value) {
        $decade = substr(date('Y', strtotime($value->date)), 0, -1)."0"; 
        $eventsDecade[$decade][] = new App\Models\Event($value->toArray());
    }

通过这样做,我创建了一个带有十年的数组,并在每个十年内创建了一个事件对象,我可以将其用作普通对象:例如,在foreach中为{{ $event->name }} 不确定您的问题到底是...

暂无
暂无

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

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