Code feed
Code worth pausing your scroll for.
Filter your feed
Community signal
Shuffle
print code
print('hello world')
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(...);
Get 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';
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',
);
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);
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();
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
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
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.
Bind values in raw queries
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Bind values in raw queries
<?php
$orders = Order::query()
->whereRaw(
'total > CASE WHEN state = ? THEN ? ELSE ? END',
['TX', 200, 100],
)
->get();
// Further reading: https://laravel-news.com/eloquent-tips-tricks