Code feed
Code worth pausing your scroll for.
Filter your feed
Community signal
ShuffleGenerate and cache embeddings
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/docs/13.x/ai-sdk#embeddings
Generate and cache embeddings
<?php
use Illuminate\Support\Str;
$embedding = Str::of($article->body)
->toEmbeddings(cache: 3600);
// Further reading: https://laravel.com/docs/13.x/ai-sdk#embeddings
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
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
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
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
Generate a complete model set
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Generate a complete model set
<?php
// Model + migration + factory + seeder + controller.
// Run this command in your terminal:
php artisan make:model Company -mfsc
// Further reading: https://laravel-news.com/eloquent-tips-tricks
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
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
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
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.