Code feed
Code worth pausing your scroll for.
Filter your feed Active
Community signal
ShuffleUse 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
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
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
Sort computed values in memory
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Sort computed values in memory
<?php
// Good for an already-small collection.
$clients = Client::query()
->limit(100)
->get()
->sortBy('full_name');
// 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
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
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