Code feed
Code worth pausing your scroll for.
Filter your feed Active
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
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
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
Read 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