Code feed
Code worth pausing your scroll for.
Filter your feed
Community signal
ShuffleExtend 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
Know whether a cache item was warm
Laravel 13 API example based on official Laravel documentation. Source: https://api.laravel.com/docs/13.x/Illuminate/Cache/Repository.html#method_rememberWithWarmth
Know whether a cache item was warm
<?php
use Illuminate\Support\Facades\Cache;
[$products, $warm] = Cache::rememberWithWarmth(
'products:featured',
now()->addHour(),
fn () => Product::featured()->get(),
);
return response()->json($products)
->header('X-Cache', $warm ? 'HIT' : 'MISS');
// Further reading: https://api.laravel.com/docs/13.x/Illuminate/Cache/Repository.html#method_rememberWithWarmth
Configure queued jobs with attributes
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/docs/13.x/releases#expanded-php-attributes
Configure queued jobs with attributes
<?php
use Illuminate\Queue\Attributes\Backoff;
use Illuminate\Queue\Attributes\Timeout;
use Illuminate\Queue\Attributes\Tries;
#[Tries(5)]
#[Backoff(10, 30, 90)]
#[Timeout(120)]
final class SyncInventory implements ShouldQueue
{
// ...
}
// Further reading: https://laravel.com/docs/13.x/releases#expanded-php-attributes