티스토리 뷰

// 리턴되는 항목들로 이루어진 새로운 콜렉션이 생성됨.
$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

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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 31
글 보관함