New Features in Laravel 12

             Laravel 12, released in February 2024, introduces a range of enhancements aimed at improving performance, developer experience, and modern PHP compatibility. Whether you're building APIs, web applications, or real-time apps, Laravel 12 continues to provide elegant solutions and developer-friendly tooling. Here's a look at the most notable Laravel 12 new features and updates.

Laravel 12 New Features with Sample Code

Table Of Content

1 Laravel 12 Requirements

Before starting the Laravel 12 install process, ensure you meet the following Laravel 12 requirements:

1. PHP ≥ 8.2 (Laravel 12 PHP version compatibility)
2. Composer
3. Database (Optional but recommended)
4. Node & npm for frontend dependency management
5. Web Server (Apache or Nginx recommended)

2 What's New in Laravel 12?

1.) Model Casts Improvements

Model casts now allow you to specify default values directly inside the cast array.

Laravel 11 - Old Way
    
    class User extends Model
    {
        protected $casts = [
            'is_active' => 'boolean',
        ];

        protected $attributes = [
            'is_active' => true, // Set default separately
        ];
    }
    
Laravel 12 - New Way
    
    class User extends Model
    {
        protected $casts = [
            'is_active' => 'boolean:default(true)',
        ];
    }
    
2.) authorizeResource improvements in Controllers

Now auto-detects the model using route model binding.

Laravel 11 - Old Way
    
    class PostController extends Controller
        {
            public function __construct()
            {
                $this->authorizeResource(Post::class, 'post');
            }
        }
    
Laravel 12 - New Way
    
    class PostController extends Controller
    {
        public function __construct()
        {
            $this->authorizeResource();
        }
    }
    

(It figures out you are using Post model from the controller name!)

3.) UpsertOrInsert for Eloquent

A new method to atomically insert or update data.

Laravel 11 - Old Way
    
    User::updateOrInsert(
        ['email' => 'example@example.com'],
        ['name' => 'Updated Name']
    );
    
Laravel 12 - New Way
    
    User::upsertOrInsert(
        ['email' => 'example@example.com'],
        ['name' => 'Updated Name']
    );
    

(Clearer intention, internally optimized)

4.) Blade Updates : @checked, @disabled, @readonly

Shorter and cleaner syntax for form attributes.

Laravel 11 - Old Way
    
    <input type="checkbox" name="active" {{ old('active', false) ? 'checked' : '' }}>
    
Laravel 12 - New Way
    
    <input type="checkbox" name="active" @checked(old('active', false))>
    <input type="text" name="name" @readonly($user->is_guest)>
    <input type="submit" value="Save" @disabled(!$user->can_edit)>
    

(Super clean and readable!)

5.) whenFilled() Method in Query Builder

Adds a condition only when the value is not empty.

Laravel 11 - Old Way
    
    $query = User::query();
    if (!empty($request->email)) {
        $query->where('email', $request->email);
    }
    
Laravel 12 - New Way
    
    $query = User::query()
        ->whenFilled('email', $request->email);
    
6.) Bulk Upsert with Auto-Detect Unique Keys

Laravel 12 detects unique keys automatically during upsert().

Laravel 11 - Old Way
    
    User::upsert([
        ['email' => 'one@example.com', 'name' => 'One'],
        ['email' => 'two@example.com', 'name' => 'Two'],
    ], ['email']); // You must mention 'email' key manually
    
Laravel 12 - New Way
    
    User::upsert([
        ['email' => 'one@example.com', 'name' => 'One'],
        ['email' => 'two@example.com', 'name' => 'Two'],
    ]);
    

(If your table already has a unique index on email, Laravel knows to use it!)

7.) New lazyByIdDesc() Method

Efficiently load large datasets in reverse order.

    
    User::lazyByIdDesc()->each(function ($user) {
        // Process each user
    });
    
8.) New MissingValue Class for Optional Data

Use MissingValue to skip null fields in API resources.

    
    use Illuminate\Http\Resources\MissingValue;
        public function toArray($request)
        {
            return [
                'name' => $this->name,
                'profile_photo' => $this->profile_photo ?? new MissingValue(),
            ];
        }
    

Tags