Because you have two $dirtyElements on that is global in your method and one that is in your closure.
$dirtyElements = array(); //The global one
$collection = $items->each(function ($item, $key) use ($dirtyElements) {
if($item->isDirty()) {
$dirtyElements[] = $item->getDirty(); // The local one in the closure.
//dd($dirtyElements); //The local one.
}
});
dd($dirtyElements) //The global one
@Tray2 your answer and your reference were very helpful. Thanks.
@Snapey your example looked like nice and clean but I'm afraid each method does not work like that. I mean, I wanted dirty array but in that way it only returned me the whole model again. But your answer helped me to resolve my problem. Thanks.