데이터베이스 쿼리 후에 모음을 가져옵니다. 그런 다음 closure 콜백을 전달하여 그룹화합니다. 그리고 나서 마지막으로 배열을 정렬하기 위해 클로저 콜백을 전달하는 각각을 사용하여 콜렉션에서 그룹화 된 배열을 정렬하고 싶습니다. 그러나 일하지 마라. 어쩌면 그것은 을 어떻게 보호 할 수 있습니까?Laravel 컬렉션의 정렬 그룹 배열
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);
정보는 $ 포스트>의 생성 날짜에 의해 주문되지
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"의 그룹을 볼 수있는 코멘트 .