Code feed
Code worth pausing your scroll for.
Filter your feed
Community signal
ShuffleApply 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
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
Pause or Resume Every Queue
Pause all Laravel queue connections during maintenance and resume them afterward.
Pause or Resume Every Queue
php artisan queue:pause --all
php artisan queue:resume --all
Query dates without raw SQL
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Query dates without raw SQL
<?php
$orders = Order::query()
->whereDate('created_at', today())
->whereYear('paid_at', now()->year)
->get();
// Further reading: https://laravel-news.com/eloquent-tips-tricks
Generate and cache embeddings
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/docs/13.x/ai-sdk#embeddings
Generate and cache embeddings
<?php
use Illuminate\Support\Str;
$embedding = Str::of($article->body)
->toEmbeddings(cache: 3600);
// Further reading: https://laravel.com/docs/13.x/ai-sdk#embeddings
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
Dispatch many jobs with Bus bulk
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/framework/docs/changelog#introduce-busbulk
Dispatch many jobs with Bus bulk
<?php
use Illuminate\Support\Facades\Bus;
Bus::bulk(
$users
->map(fn (User $user) => new RefreshProfile($user))
->all(),
);
// Further reading: https://laravel.com/framework/docs/changelog#introduce-busbulk
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