Code feed
Code worth pausing your scroll for.
Filter your feed Active
Community signal
Shuffle
Bb
Jjjjj;
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',
],
Update counters atomically
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Update counters atomically
<?php
$post = Post::findOrFail($postId);
$post->increment('views');
$post->increment('score', 5);
$post->decrement('stock');
// Further reading: https://laravel-news.com/eloquent-tips-tricks
Find it or create it
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Find it or create it
<?php
$user = User::firstOrCreate(
['email' => $email],
['name' => $name],
);
$order = Order::findOrFail($orderId);
// Further reading: https://laravel-news.com/eloquent-tips-tricks
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
Keep model configuration explicit
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Keep model configuration explicit
<?php
protected $fillable = ['name', 'email'];
protected $hidden = ['password'];
protected function casts(): array
{
return ['email_verified_at' => 'datetime'];
}
// Further reading: https://laravel-news.com/eloquent-tips-tricks
Fetch several IDs at once
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Fetch several IDs at once
<?php
$users = User::findMany([1, 4, 9]);
$emails = $users->pluck('email');
// 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
Get the latest related model
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Get the latest related model
<?php
public function latestPost(): HasOne
{
return $this->hasOne(Post::class)
->latestOfMany();
}
// 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