Topics Laravel 13 New Features New Helpers & Improvements
intermediate 14 min read

New Helpers & Improvements

Cache::flexible(), Cache::memo(), Number helpers, Str improvements, and new artisan commands.

Cache::flexible()

use Illuminate\Support\Facades\Cache;

// Cache stampede protection with probabilistic early expiration
$value = Cache::flexible("expensive_data", [30, 60], function () {
return expensiveComputation();
});

// [30, 60] = stale after 30s, recalculated by one process at random up to 60s

Cache::memo()

// Memoize function results for the current request
$result = Cache::memo("user_".$id, function () use ($id) {
return User::with("posts")->find($id);
}, $seconds = 3600);

Number & Str Helpers

use Illuminate\Support\Number;

// Number helpers
echo Number::format(1234567); // 1,234,567
echo Number::percentage(75); // 75%
echo Number::currency(1000); // $1,000.00
echo Number::fileSize(1048576); // 1 MB

// Str improvements
echo str(" Hello ")->inlineMarkdown(); // <p>Hello</p>
echo str()->isUrl("https://laravel.com"); // true

New Artisan Commands

// php artisan config:show app.name
// php artisan make:class SomeClass
// php artisan make:enum Status
// php artisan make:interface Logger
// php artisan make:trait Timestampable

Model Attributes

// Laravel 13: #[Fillable] replaces $fillable
// Laravel 13: #[Appends] replaces $appends
// Laravel 13: #[UseResource] replaces toResource()
// Laravel 13: #[ScopedBy] replaces addGlobalScope()

Examples

<?php
use Illuminate\Support\Facades\Cache;

// Cache::flexible() with stampede protection
\$data = Cache::flexible('trending_posts', [30, 120], function () {
    return \App\Models\Post::where('is_published', true)
        ->orderByDesc('views')
        ->limit(10)
        ->get();
});

echo \$data->count() . ' trending posts.';

// Cache::memo() for request-scoped memoization
\$user = Cache::memo('user_profile', fn() => auth()->user()->load('profile'), 300);

Your Notes

Sign in to take notes for this lesson.

Discussion

Sign in to join the discussion.

Flashcards

Sign in to create flashcards.