I'm having a problem removing an item from a collection. I have a model called Class that represents a class of students, and that class can have many instances. I'm trying to remove classes that have no space left, but I keep getting the full set of instances.
$class->instances = $class->instances->reject(function($instance) {
$registrations = $instance->registrations()->get();
$instance->spaces_left = $instance->max_students - count($registrations);
if($instance->spaces_left == 0) {
return true;
}
});
The weird thing is that if I assign the results of that reject function to something called $class-instances2 or any other name, it works just fine.
Figured out my own problem. I never called ->get() or ->first() on class. Doing that before this statement solved this issues.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community