Code feed
Code worth pausing your scroll for.
Filter your feed
Community signal
ShuffleRead the affected row count
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Read the affected row count
<?php
$updated = Product::query()
->whereNull('category_id')
->update(['category_id' => $fallbackCategoryId]);
logger()->info("Updated {$updated} products");
// Further reading: https://laravel-news.com/eloquent-tips-tricks
Keep OR branches grouped
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Keep OR branches grouped
<?php
$matches = Record::query()
->where('tenant_id', $tenantId)
->where(fn ($q) => $q
->where('status', 'ready')
->orWhere('priority', 'high'))
->get();
// Further reading: https://laravel-news.com/eloquent-tips-tricks
Process big tables in chunks
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Process big tables in chunks
<?php
User::query()->chunkById(500, function ($users): void {
foreach ($users as $user) {
SyncProfile::dispatch($user);
}
});
// Further reading: https://laravel-news.com/eloquent-tips-tricks
Save without touching timestamps
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Save without touching timestamps
<?php
$product->forceFill([
'updated_at' => '2025-01-01 10:00:00',
]);
$product->save(['timestamps' => false]);
// Further reading: https://laravel-news.com/eloquent-tips-tricks
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
Group mixed AND and OR conditions
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Group mixed AND and OR conditions
<?php
$people = Person::query()
->where(fn ($q) => $q
->where('age', '>=', 18)
->where('plan', 'adult'))
->orWhere(fn ($q) => $q
->where('age', '>=', 65)
->where('plan', 'senior'))
->get();
// 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
Bb
Jjjjj;
Hello, CodeSnap!
const greet = name => {
return `Hello, ${name}!`;
};
console.log(greet('developer'));
Hello, CodeSnap!
const greet = name => {
return `Hello, ${name}!`;
};
console.log(greet('developer'));