# Laravel Essentials: Supercharge Your Laravel Development with Better Defaults

By Dinesh Uprety
Published: 2025-06-06
Updated: 2025-06-07
Canonical: https://laranepal.com/blog/laravel-essentials-supercharge-your-laravel-development-with-better-defaults

## Introduction

Laravel is already a powerful and developer-friendly PHP framework, but what if you could make it even better with **sensible defaults, stricter models, and automated optimizations**? That's where **[Nuno Maduro's Essentials](https://github.com/nunomaduro/essentials)** comes in.

This package enhances Laravel with **strict mode for Eloquent models, auto-eager loading, immutable dates, and more**,
helping developers avoid common pitfalls while improving performance and security.

Built for **Laravel 11+ and PHP 8.3+**, Essentials is designed for modern Laravel applications, enforcing best practices
while keeping things flexible.

---

## Key Features & Benefits

### ✅ 1. Strict Models (No More Silent Failures!)

By default, Laravel silently ignores undefined model attributes and lazy-loaded relationships. Essentials changes this
by:

- **Throwing exceptions** when accessing undefined attributes
- **Blocking lazy loading** (unless explicitly allowed)
- **Preventing invalid assignments**

**Why?** Fewer hidden bugs and more predictable model behavior.

```php
// Throws an exception if 'non_existent_column' doesn't exist
$user->non_existent_column;

```

### ⚡ 2. Auto Eager Loading (Bye, N+1 Queries!)

Forgets to add `with()`? Essentials automatically eager loads relationships defined in `$with`, reducing `N+1` issues.

```php
class User extends Model
{
    protected $with = ['posts']; // Always eager-loaded
}
```

### 🕒 3. Immutable Dates (No More Surprises!)

Replaces Laravel's mutable `Carbon` dates with `CarbonImmutable`, preventing accidental date mutations.

```php
$user->created_at->addDay(); // Returns a new instance instead of modifying
```

### 🔒 4. Force HTTPS (Security First!)

Ensures **all generated URLs use HTTPS**, improving security by default.
Internally package uses `URL::forceHttps()` to enforce secure URLs in production.

```php
<?php

use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\App;

private function configureUrl(): void
{
   URL::forceHttps(App::isProduction());
}
```

### 🛑 5. Safe Console (Avoid Production Disasters!)

Blocks dangerous Artisan commands (like **migrate:fresh**) in production.

```php
// Prevents accidental `php artisan migrate:fresh` in production
```

Much more features are available, including visit [Nuno Maduro's Essentials](https://github.com/nunomaduro/essentials).

---

## Artisan Commands for Better Workflow

### 🏗️ `make:action` (Clean Business Logic)

Generates **single-responsibility action classes** (great for moving logic out of controllers).

```bash
php artisan make:action CreateUserAction
```

#### Output:

```php
final readonly class CreateUserAction
{
    public function handle(): void
    {
        DB::transaction(function (): void {
            // Business logic here
        });
    }
}
```

### 🎨 `essentials:pint` (**Stricter Code Standards**)

Enhances Laravel Pint with **stricter rules**:

* **Strict types** (`declare(strict_types=1)`)
* **Final classes by default**
* **Strict comparisons** (`===` instead of `==`)

```bash
php artisan essentials:pint --force
```

### 🔄 `essentials:rector` (Automated Refactoring)

Applies **PHP modernization rules**:

* **Dead code removal**
* **Type declarations**
* **Early returns**

```bash
php artisan essentials:rector
```

---

## Installation & Setup

1. **Require via Composer** (Laravel 11+ & PHP 8.3+ required):

    ```php
    composer require nunomaduro/essentials:^0.1
    ```

2. **Publish config** (optional, to customize features):

    ```bash
    php artisan vendor:publish --tag=essentials-stubs
    ```

3. **Enable/disable** features in `config/essentials.php`:

    ```bash
    return [
        ShouldBeStrict::class => true, // Strict models
        Unguard::class => false, // Disable mass assignment protection?
        // ...
    ];
    ```

---

## When Should You Use Essentials?

* ✅ **New Laravel projects** – Start with best practices from day one
* ✅ **Strict mode lovers** – Prevents silent failures in models
* ✅ **Performance-focused apps** – Auto eager-loading & prefetching help
* ✅ **Teams enforcing standards** – Pint & Rector improve code quality

⚠️ **Not recommended for legacy apps** (unless you're ready for breaking changes)

---

## Conclusion

**Laravel Essentials** is a game-changer for modern Laravel development, providing sensible defaults and automated
optimizations that make your applications more robust, secure, and performant. By enforcing strict models, auto eager
loading, immutable dates, and more, Essentials helps you avoid common pitfalls while keeping your code clean and
maintainable.

---
