Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

No, there is nothing wrong with doing it like that.

Last updated 2 years ago.
0

I guess it depends on collection size if it is big, there may be performance issues for sure :)

Last updated 2 years ago.
0

How big, and how come?

Last updated 2 years ago.
0

did you read http://laravel.com/docs/eloquent#collections regarding filter() and each()

Last updated 2 years ago.
0

It seems like doing it that way is kind of fuzzy and procedural. Instead, you could define a custom collection that would automatically do the work for you. For example:

class Collection extends Illuminate\Support\Collection
{
    public function cancelled()
    {
        return $this->filter(function($order) {
            return $order->isCancelled();
        }
    }
}

And now, you can get your cancelled orders just by doing this:

$orders = Order::somethingthatresultsinacollectionobject();
foreach($orders->cancelled() as $order) {
    //do whatever
}

See: http://laravel.com/docs/eloquent#collections

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

mjelle mjelle Joined 7 Apr 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.

© 2024 Laravel.io - All rights reserved.