Code feed
Code worth pausing your scroll for.
Filter your feed Active
Community signal
ShufflePHP 8.5 Pipe Operator
Pass a value through multiple functions without temporary variables or nested calls.
PHP 8.5 Pipe Operator
<?php
$title = ' PHP 8.5 Released ';
$slug = $title
|> trim(...)
|> (fn ($value) => str_replace(' ', '-', $value))
|> strtolower(...);
Configure Read-Through File Storage
Read from an old disk and automatically copy missing files to the new primary disk.
Configure Read-Through File Storage
'assets' => [
'driver' => 'read-through',
'primary' => 'r2',
'fallback' => 'legacy-s3',
],
Extend a cache item without reading it
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/docs/13.x/cache#extending-item-lifetime
Extend a cache item without reading it
<?php
use Illuminate\Support\Facades\Cache;
$extended = Cache::touch(
"checkout:{$cart->id}",
now()->addMinutes(30),
);
// Further reading: https://laravel.com/docs/13.x/cache#extending-item-lifetime
Route job classes centrally
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/docs/13.x/queues#queue-routing
Route job classes centrally
<?php
use App\Jobs\GenerateReport;
use Illuminate\Support\Facades\Queue;
Queue::route(
GenerateReport::class,
connection: 'redis',
queue: 'reports',
);
// Further reading: https://laravel.com/docs/13.x/queues#queue-routing
Prompt a first-party AI agent
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/docs/13.x/ai-sdk
Prompt a first-party AI agent
<?php
use App\Ai\Agents\CodeReviewer;
$review = CodeReviewer::make()->prompt(
'Review this pull request for risky changes.',
);
return (string) $review;
// Further reading: https://laravel.com/docs/13.x/ai-sdk
Use model events for defaults
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Use model events for defaults
<?php
protected static function booted(): void
{
static::creating(function (Post $post): void {
$post->uuid ??= (string) Str::uuid();
});
}
// Further reading: https://laravel-news.com/eloquent-tips-tricks
Search by semantic similarity
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/docs/13.x/releases#semantic-vector-search
Search by semantic similarity
<?php
use Illuminate\Support\Facades\DB;
$articles = DB::table('articles')
->whereVectorSimilarTo('embedding', $query)
->limit(8)
->get();
// Further reading: https://laravel.com/docs/13.x/releases#semantic-vector-search
Hydrate eager-loaded parents on children
Laravel 12: prevent child-to-parent N+1 queries with chaperone(). Source: laravel.com/docs/12.x/eloquent-relationships
Hydrate eager-loaded parents on children
<?php
use Illuminate\Database\Eloquent\Relations\HasMany;
public function comments(): HasMany
{
return $this->hasMany(Comment::class)->chaperone();
}
// $comment->post is already hydrated after eager loading.
Apply a global query scope
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Apply a global query scope
<?php
protected static function booted(): void
{
static::addGlobalScope('active', fn ($query) =>
$query->where('is_active', true)
);
}
// Further reading: https://laravel-news.com/eloquent-tips-tricks
Provide a default related model
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Provide a default related model
<?php
public function author(): BelongsTo
{
return $this->belongsTo(User::class)
->withDefault(['name' => 'Guest Author']);
}
// Further reading: https://laravel-news.com/eloquent-tips-tricks