php
74 lines · 2 tabs
Carlos Mendez
Jan 2026
2 tabs
<?php
use Illuminate\Support\Collection;
// Create collection
$posts = collect(Post::all());
// Transform data
$published = $posts
->filter(fn ($post) => $post->is_published)
->map(fn ($post) => [
'title' => $post->title,
'author' => $post->author->name,
'url' => route('posts.show', $post),
])
->values(); // Reset keys
// Group by author
$byAuthor = $posts->groupBy('author.name');
// Get unique tags
$tags = $posts
->pluck('tags')
->flatten()
->unique()
->sort()
->values();
// Partition into two collections
[$published, $drafts] = $posts->partition(
fn ($post) => $post->is_published
);
// Complex aggregation
$stats = $posts
->groupBy('category')
->map(fn ($group) => [
'count' => $group->count(),
'avg_views' => $group->avg('views'),
'total_comments' => $group->sum('comments_count'),
]);
// Higher-order messages
$totalPosts = $users->sum->posts->count();
$names = $users->map->name;
$published = $posts->filter->is_published;
// Chunk processing
$posts->chunk(100)->each(function ($chunk) {
// Process 100 posts at a time
$chunk->each->update(['processed' => true]);
});
<?php
use Illuminate\Support\Collection;
// Extend Collection with custom methods
Collection::macro('toSelectArray', function () {
return $this->mapWithKeys(function ($item) {
return [$item->id => $item->name];
})->toArray();
});
// Usage
$options = User::all()->toSelectArray();
// ['1' => 'John', '2' => 'Jane', ...]
// Pipe through custom transformation
$result = $posts->pipe(function ($collection) {
return $collection
->filter->is_featured
->take(5)
->values();
});
2 files · php
Explain with highlit
Laravel collections provide a fluent, powerful API for working with arrays. Every Eloquent query returns a collection, but I also create collections from arrays with collect(). Methods like map(), filter(), reduce(), groupBy(), and sortBy() transform data without loops. Collections are lazy—operations don't execute until you call terminal methods like toArray() or values(). The pipe() method passes collections through custom transformations. Higher-order messages simplify common operations—$users->sum->posts->count() sums post counts across users. Collections make complex data transformations readable and chainable. I prefer collections over manual array manipulation for cleaner, more expressive code.
Related snips
php
<?php
namespace App\Providers;
use App\Contracts\PaymentGateway;
use App\Services\StripePaymentGateway;
Laravel service container and dependency injection
laravel
dependency-injection
service-container
by Carlos Mendez
2 tabs
json
{
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build"
},
Laravel mix/Vite for asset compilation
laravel
vite
assets
by Carlos Mendez
4 tabs
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Laravel database migrations for schema management
laravel
migrations
database
by Carlos Mendez
3 tabs
php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
Laravel form requests for validation
laravel
validation
form-requests
by Carlos Mendez
2 tabs
php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Laravel soft deletes for data retention
laravel
soft-deletes
eloquent
by Carlos Mendez
3 tabs
php
<?php
namespace App\Notifications;
use App\Models\Post;
use Illuminate\Bus\Queueable;
Laravel notifications for multi-channel messaging
laravel
notifications
email
by Carlos Mendez
2 tabs
Share this code
Here's the card — post it anywhere.