Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent

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.

Last updated 2 years ago.
0

Figured out my own problem. I never called ->get() or ->first() on class. Doing that before this statement solved this issues.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

joetito1 joetito1 Joined 16 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.