Code feed
Code worth pausing your scroll for.
Filter your feed
Community signal
ShuffleGet the First and Last Array Values
PHP 8.5 adds native functions that return null for empty arrays.
Get the First and Last Array Values
<?php
$firstUser = array_first($users);
$lastUser = array_last($users);
$firstUser ??= 'No users found';
PHP 8.5 Pipe Operator
Pass a value through multiple functions without temporary variables or nested calls.
PHP 8.5 Pipe Operator
<?php
$title = ' PHP 8.5 Released ';
$slug = $title
|> trim(...)
|> (fn ($value) => str_replace(' ', '-', $value))
|> strtolower(...);
Warn When a Return Value Is Ignored
Use #[NoDiscard] when callers should not accidentally ignore an important result.
Warn When a Return Value Is Ignored
#[\NoDiscard]
function saveOrder(Order $order): bool
{
return $order->save();
}
$saved = saveOrder($order);
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',
],
Route Laravel Jobs Centrally
Define the default connection and queue for a job class in one place.
Route Laravel Jobs Centrally
use App\Jobs\ProcessPodcast;
use Illuminate\Support\Facades\Queue;
Queue::route(
ProcessPodcast::class,
connection: 'redis',
queue: 'podcasts',
);
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
Debounce Queued Event Listeners
Process only the newest event when several events arrive during a short period.
Debounce Queued Event Listeners
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\Attributes\DebounceFor;
#[DebounceFor(30, maxWait: 120)]
class UpdateProductSearchIndex implements ShouldQueue
{
public function debounceId(ProductUpdated $event): string
{
return (string) $event->product->getKey();
}
}
Perform Semantic Search with Scout
Find records by meaning instead of relying only on exact keyword matches.
Perform Semantic Search with Scout
$documents = Document::search('Laravel queue guide')
->semantic()
->where('published', true)
->get();
Cast Database Vectors in Eloquent
Convert vector columns into PHP arrays automatically using Laravel’s AsVector cast.
Cast Database Vectors in Eloquent
use Illuminate\Database\Eloquent\Casts\AsVector;
use Illuminate\Database\Eloquent\Model;
class Document extends Model
{
protected function casts(): array
{
return [
'embedding' => AsVector::class,
];
}
}
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