Code feed
Code worth pausing your scroll for.
Filter your feed
Community signal
ShuffleRoute job classes centrally
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/docs/13.x/queues#queue-routing
Route job classes centrally
<?php
use App\Jobs\GenerateReport;
use Illuminate\Support\Facades\Queue;
Queue::route(
GenerateReport::class,
connection: 'redis',
queue: 'reports',
);
// Further reading: https://laravel.com/docs/13.x/queues#queue-routing
Build conditional queries fluently
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Build conditional queries fluently
<?php
$users = User::query()
->when($role, fn ($query, $role) =>
$query->where('role', $role)
)
->latest()
->get();
// Further reading: https://laravel-news.com/eloquent-tips-tricks
Secure controllers with attributes
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/docs/13.x/releases#expanded-php-attributes
Secure controllers with attributes
<?php
use Illuminate\Routing\Attributes\Controllers\Authorize;
use Illuminate\Routing\Attributes\Controllers\Middleware;
#[Middleware('auth')]
final class InvoiceController
{
#[Authorize('view', 'invoice')]
public function show(Invoice $invoice): Invoice
{
return $invoice;
}
}
// Further reading: https://laravel.com/docs/13.x/releases#expanded-php-attributes
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
Find the first matching array value
PHP 8.4 tip: use array_find() instead of a manual search loop. Source: php.net/releases/8.4
Find the first matching array value
<?php
$admin = array_find(
$users,
static fn (User $user): bool => $user->isAdmin(),
);
// $admin is the first match, or null.
Build multi-type JSON schemas
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/framework/docs/changelog#add-multi-type-union-support-to-illuminatejsonschema
Build multi-type JSON schemas
<?php
use Illuminate\JsonSchema\JsonSchema;
$identifier = JsonSchema::union([
'string',
'number',
])->nullable();
// Further reading: https://laravel.com/framework/docs/changelog#add-multi-type-union-support-to-illuminatejsonschema
Attach structured metadata to routes
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/framework/docs/changelog#add-route-metadata-support
Attach structured metadata to routes
<?php
Route::get('/pricing', PricingController::class)
->metadata([
'seo' => ['title' => 'Pricing', 'robots' => 'index'],
'feature' => 'public-pricing',
]);
$title = request()->route()->getMetadata('seo.title');
// Further reading: https://laravel.com/framework/docs/changelog#add-route-metadata-support
Read filled request enums safely
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/framework/docs/changelog#add-whenfilledenum-method-to-interactswithdata
Read filled request enums safely
<?php
$request->whenFilledEnum(
'status',
OrderStatus::class,
fn (OrderStatus $status) => $query->where('status', $status),
);
// Further reading: https://laravel.com/framework/docs/changelog#add-whenfilledenum-method-to-interactswithdata
Migrate storage with a read-through disk
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/docs/13.x/filesystem#scoped-read-only-and-read-through-filesystems
Migrate storage with a read-through disk
<?php
// config/filesystems.php
'assets' => [
'driver' => 'read-through',
'primary' => 'r2',
'fallback' => 'legacy-s3',
'throw_on_promotion_failure' => false,
],
// A fallback read is promoted to the primary disk.
$logo = Storage::disk('assets')->get('logos/app.svg');
// Further reading: https://laravel.com/docs/13.x/filesystem#scoped-read-only-and-read-through-filesystems
Inspect HTTP fake request URIs
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/framework/docs/changelog#add-clientrequesturi
Inspect HTTP fake request URIs
<?php
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
Http::assertSent(fn (Request $request) =>
$request->uri()->path() === '/api/users'
&& $request->uri()->query()->integer('page') === 2
);
// Further reading: https://laravel.com/framework/docs/changelog#add-clientrequesturi