티스토리 뷰
// 리턴되는 항목들로 이루어진 새로운 콜렉션이 생성됨.
$resultCollection = $collection->map(function (Model $model) {
return $model->asdf;
}
// 조건에 해당하면 해당 값 리턴해서 새로운 콜렉션 생성됨.
$resultCollection = $collection->filter(function (Model $model) {
return !$model->asdf
}
// 매뉴얼에서 예제
$collection = collect([1, 2, 3, 4]);
$filtered = $collection->filter(function ($value, $key) {
return $value > 2;
});
$filtered->all();
// [3, 4]
// 해당 콜렉션 아이템을 차례대로 실행함. 일괄적으로 뭔가를 해야할때 사용.
$collection->each(function (Model $model) use ($data) {
$model->asdf = $data->asdf;
}
// 콜렉션 내에 아이템을 검색해서 데이터를 넣는 것이므로 콜렉션내에 index값이 순서대로이지 않음.
// 순서대로 다시 index 주는게 values()
$filteredCollection->each(function (Model $model) use ($originCollection) {
$originCollect = $originCollection->where('origin_id', $model->id)->first();
$model->asdf = $originCollect->asdf;
$model->qwer = $originCollect->qwer;
})->values();
https://laravel.kr/docs/9.x/eloquent-collections#Eloquent:%20Collections
'Laravel' 카테고리의 다른 글
[laravel8] 슬랙 알림 보내기 (0) | 2023.01.16 |
---|---|
결제 - 아임포트 (0) | 2023.01.11 |
[laravel8] $model->unsetRelation() (0) | 2022.11.01 |
[laravel8] sail로 여러개 프로젝트를 띄워보자. (0) | 2022.10.27 |
laravel sail - 루트로 bash에 접속해보자. : > sail root-shell (0) | 2022.10.19 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 배열을_이쁘게
- Testing
- 프로그래밍은디버거부터시작이다
- fmf
- 엘라스틱서치한글성공!
- twoseven.kr/0410
- JSON으로도
- 테스팅환경
- twoserven.kr/0410
- 테스팅
- gcp
- xml로도
- PhpStorm
- 더미데이터도유형이있어요
- UTF8
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함