@markdown# Don’t Filter by NULL in CollectionsYou can filter by NULL in Eloquent, but if you're filtering the **collection** further - filter by empty string, there's no "null" in that field anymore.```php// This works$messages = Message::where('read_at is null')->get();// Won’t work - will return 0 messages$messages = Message::all();$unread_messages = $messages->where('read_at is null')->count();// Will work$unread_messages = $messages->where('read_at', '')->count();```### Use groupBy on Collections with Custom Callback FunctionIf you want to group result by some condition which isn’t a direct column in your database, you can do that by providing a closure function.For example, if you want to group users by day of registration, here’s the code:```php$users = User::all()->groupBy(function($item) { return $item->created_at->format('Y-m-d');});```⚠️ Notice: it is done on a `Collection` class, so performed **AFTER** the results are fetched from the database.### Multiple Collection Methods in a RowIf you query all results with `->all()` or `->get()`, you may then perform various Collection operations on the same result, it won’t query database every time.```php$users = User::all();echo 'Max ID: ' . $users->max('id');echo 'Average age: ' . $users->avg('age');echo 'Total budget: ' . $users->sum('budget');```### Calculate Sum with PaginationHow to calculate the sum of all records when you have only the PAGINATED collection? Do the calculation BEFORE the pagination, but from the same query.```php// How to get sum of post_views with pagination?$posts = Post::paginate(10);// This will be only for page 1, not ALL posts$sum = $posts->sum('post_views');// Do this with Query Builder$query = Post::query();// Calculate sum$sum = $query->sum('post_views');// And then do the pagination from the same query$posts = $query->paginate(10);```### Serial no in foreach loop with paginationWe can use foreach collection items index as serial no (SL) in pagination.```php ... Serial ... @foreach ($products as $product) {{ $loop->index + $product->firstItem() }} ... @endforeach```it will solve the issue of next pages(?page=2&...) index count from continue.### Higher order collection methodsCollections have higher order methods, this are methods that can be chained , like `groupBy()` , `map()` ... Giving you a fluid syntax. This example calculates theprice per group of products on an offer.```php$offer = [ 'name' => 'offer1', 'lines' => [ ['group' => 1, 'price' => 10], ['group' => 1, 'price' => 20], ['group' => 2, 'price' => 30], ['group' => 2, 'price' => 40], ['group' => 3, 'price' => 50], ['group' => 3, 'price' => 60] ]]; $totalPerGroup = collect($offer->lines)->groupBy('group')->map(fn($group) => $group->sum('price')); ```### Higher order collection messageCollections also provide support for "higher order messages", which are short-cuts for performing common actions on collections.This example calculates the price per group of products on an offer.```php$offer = [ 'name' => 'offer1', 'lines' => [ ['group' => 1, 'price' => 10], ['group' => 1, 'price' => 20], ['group' => 2, 'price' => 30], ['group' => 2, 'price' => 40], ['group' => 3, 'price' => 50], ['group' => 3, 'price' => 60] ]]; $totalPerGroup = collect($offer['lines'])->groupBy->group->map->sum('price');```### Get an existing key or insert a value if it doesn't exist and return the valueIn Laravel 8.81 `getOrPut` method to Collections that simplifies the use-case where you want to either get an existing key or insert a value if it doesn't exist and return the value.```php$key = 'name';// Still validif ($this->collection->has($key) === false) { $this->collection->put($key, ...);}return $this->collection->get($key);// Using the `getOrPut()` method with closurereturn $this->collection->getOrPut($key, fn() => ...);// Or pass a fixed valuereturn $this->collection->getOrPut($key, $value='teacoders');```Tip given by [@Teacoders](https://twitter.com/Teacoders/status/1488338815592718336)### Static times methodThe static times method creates a new collection by invoking the given closure a specified number of times.```phpCollectable::times(7, function ($number) { return now()->addDays($number)->format('d-m-Y');});// Output: [01-04-2022, 02-04-2022, ..., 07-04-2022]```Tip given by [@Teacoders](https://twitter.com/Teacoders/status/1509447909602906116)@endmarkdown