Your Guide to Laravel Excellence

New in Laravel 12: Eager Loading, Attribute Scopes, and fromJson

New in Laravel 12: Eager Loading, Attribute Scopes, and fromJson

⚡Eager Loading Improvements

When working with nested relationships like users -> posts -> comments, you can now eager load deeply related data in just one query set:

$users = User::with('posts.commments')->get();
foreach ($users as $user) {
		foreach ($user->posts as $post) {
        echo $post->commets;
     }
		echo $user->name;
}

Laravel 12 also supports automatic eager loading, removing the need to manually call with() each time:

        $users = User::all();

        $users->withRelationshipAutoLoading();

        foreach ($users as $user) {
            foreach ($user->posts as $post) {
               echo $post->commets;
            }
            echo $user->name;
        }

Global Auto Eager Loading

To enable eager loading for all models globally, set it in your AppServiceProvider:

public function boot()
{
    Model::automaticallyEagerLoadRelationships();
}

Attribute Scopes

In Laravel, query scopes let you extract common query logic into reusable methods on your Eloquent models. Laravel 12 introduced a cleaner and more modern way to define these scopes using attributes.

Previously, you had to prefix the method name with scope and use it like this:


// In the model
protected function scopeActive(Builder $query): void
{
    $query->where('status', 'active');
}

// Usage
Post::active()->get();

Laravel 12 introduced a native PHP attribute called #[Scope] that simplifies this:

#[Scope]
protected function active(Builder $query): void
{
    $query->where('status', 'active');
}

$users = User::active()->get();

No more scope prefix needed. It’s cleaner, modern, and easier to understand.

Collection::fromJson()

$electronics = '[
			{
				"id": 1,
				"name": "Apple iPhone 15 Pro",
				"category": "Smartphone",
				"brand": "Apple",
				"price": 1099.99,
				"in_stock": true,
			},
			{
				"id": 2,
				"name": "Samsung Galaxy S24 Ultra",
				"category": "Smartphone",
				"brand": "Samsung",
				"price": 1199.99,
				"in_stock": true,
			}
		]';

Earlier, to convert a JSON string to a collection, you had to decode it first:

$output  = json_decode($electronics, true);
collect($output);    // old way

Now in Laravel 12.8, it’s even simpler:

Collection::fromJson($electronics);  // new way

This is a more expressive and elegant solution to handle JSON data directly as a Laravel Collection.

Recommeded Posts

How to Accept Credit Card Payments in Laravel 12 Using Square

How to Accept Credit Card Payments in Laravel 12 Using Square

Learn how to easily add Square credit card payments to your Laravel 12 website. Step-by-step guide for secure and smooth payment integration.

1 month ago Read article →
Model Change Tracking in Laravel 11 - Laravel Auditing

Model Change Tracking in Laravel 11 - Laravel Auditing

Model Change Tracking in Laravel 11 - Laravel Auditing

2 months ago Read article →
CRUD with Alpine.js, Tailwind CSS, and jQuery DataTable Modals

CRUD with Alpine.js, Tailwind CSS, and jQuery DataTable Modals

Learn how to build a full CRUD system using Alpine.js, Tailwind CSS, and jQuery DataTable, complete with modals for better user experience.

2 months ago Read article →
Google Oauth Authentication from Scratch in Laravel

Google Oauth Authentication from Scratch in Laravel

Google Oauth Authentication from Scratch in Laravel

2 months ago Read article →