PHPackages                             kainiklas/laravel-strict-mode - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. kainiklas/laravel-strict-mode

ActiveLibrary[Database &amp; ORM](/categories/database)

kainiklas/laravel-strict-mode
=============================

Laravel Package to Enable Eloquent 'Strict Mode' and Further Safety Methods.

2.0.0(3y ago)68[2 PRs](https://github.com/kainiklas/laravel-strict-mode/pulls)MITPHPPHP ^8.1

Since Oct 20Pushed 2y ago1 watchersCompare

[ Source](https://github.com/kainiklas/laravel-strict-mode)[ Packagist](https://packagist.org/packages/kainiklas/laravel-strict-mode)[ Docs](https://github.com/kainiklas/laravel-strict-mode)[ RSS](/packages/kainiklas-laravel-strict-mode/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (13)Versions (9)Used By (0)

Laravel Package to Enable Eloquent "Strict Mode" and Further Safety Methods
===========================================================================

[](#laravel-package-to-enable-eloquent-strict-mode-and-further-safety-methods)

[![Laravel Strict Mode](art/laravel-strict-mode.png)](art/laravel-strict-mode.png)

[![Latest Version on Packagist](https://camo.githubusercontent.com/1ee4ad410a99610b9f79f84f99c5aa52b064821abeb7755d14056df08760b196/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b61696e696b6c61732f6c61726176656c2d7374726963742d6d6f64652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/kainiklas/laravel-strict-mode)[![GitHub Tests Action Status](https://camo.githubusercontent.com/67efb491a392db61b8424af52a9df4f3035a75e44ee3630c006b2ebe9065baaf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6b61696e696b6c61732f6c61726176656c2d7374726963742d6d6f64652f72756e2d74657374732e796d6c3f6272616e63683d6d61696e)](https://github.com/kainiklas/laravel-strict-mode/actions?query=workflow%3Arun-tests+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/cfc80ea1b946993cf6bbdf93c73da6bbc2dfc28f811e592ac2ab909fb24002a5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6b61696e696b6c61732f6c61726176656c2d7374726963742d6d6f64652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/kainiklas/laravel-strict-mode)

Enables the following configurable eloquent "strict mode" features:

- Prevent Lazy Loading (N+1)
    - Non-production: Throws an `Illuminate\Database\LazyLoadingViolationException` exception
    - Production: Writes a warning into the logs
- Prevent defaulting to NULL when using a model's attribute that hasn't been fetched from the DB or doesn't exist on the model
    - Throws an `Illuminate\Database\Eloquent\MissingAttributeException` exception (all environments)
    - Alternatively: You can set the config to write a warning to the log
- Prevent loosing attributes when creating or updating models because of missing attributes in the $fillable array
    - Throws an `Illuminate\Database\Eloquent\MassAssignmentException` exception (all environments)
    - Alternatively: You can set the config to write a warning to the log

Enables the following configurable safety methods:

- Long-running command monitoring
    - Writes a warning into the logs including the command name, user and duration which took longer than the specified threshold (default: 5000ms)
- Long-running requests monitoring
    - Writes a warning into the logs including the request url, user and duration which took longer than the specified threshold (default: 5000ms)
- Long-running single DB query monitoring
    - Writes an info into the logs including the SQL query and duration which took longer than the specified threshold (default: 1000ms)
- Long-running DB connection monitoring
    - Writes an info into the logs including the DB connection name which took longer than the specified threshold (default: 2000ms)
- Memory Heap Size monitoring
    - Writes a warning into the logs including when the memory heap size exceeds the specified threshold (default: 50MB)

Installation
------------

[](#installation)

**Laravel Version ^10.0 required.**

You can install the package via composer:

```
composer require kainiklas/laravel-strict-mode
```

Optionally, you can publish the config file.

```
php artisan vendor:publish --tag="laravel-strict-mode-config"
```

You can influence the behaviour with environment variables.

This is the contents of the published config file.

```
return [

    /**
     * Throws Illuminate\Database\LazyLoadingViolationException if model is lazy loaded.
     * Exception is only thrown if log_lazy_loading is set to false.
     */
    'prevent_lazy_loading' => env(
        'PREVENT_LAZY_LOADING',
        true,
    ),

    /**
     * Lazy Loading violation is logged. No exception is thrown.
     * Only works, if prevent_lazy_loading is true.
     */
    'log_lazy_loading' => env(
        'LOG_LAZY_LOADING',
        env('APP_ENV') == 'production'
    ),

    /**
     * Prevent non-fillable attributes from being silently discarded.
     * Instead, throws Illuminate\Database\Eloquent\MassAssignmentException.
     * Exception is only thrown if log_prevent_silently_discarding_attributes is false.
     */
    'prevent_silently_discarding_attributes' => env(
        'PREVENT_SILENTLY_DISCARDING_ATTRIBUTES',
        true
    ),

    /**
     * Log warning Illuminate\Database\Eloquent\MassAssignmentException
     * instead of throwing the exception.
     * Only works if prevent_silently_discarding_attributes is true.
     */
    'log_prevent_silently_discarding_attributes' => env(
        'LOG_PREVENT_SILENTLY_DISCARDING_ATTRIBUTES',
        false
    ),

    /**
     * If activated an Illuminate\Database\Eloquent\MissingAttributeException
     * is thrown whenever an attribute is accessed which is not present in the model,
     * instead of falling back to NULL.
     *
     * Exception is only thrown if log_prevent_accessing_missing_attributes is false.
     */
    'prevent_accessing_missing_attributes' => env(
        'PREVENT_ACCESSING_MISSING_ATTRIBUTES',
        true
    ),

    /**
     * Log warning Illuminate\Database\Eloquent\MissingAttributeException
     * instead of throwing the exception.
     * Only works if prevent_accessing_missing_attributes is true.
     */
    'log_prevent_accessing_missing_attributes' => env(
        'LOG_PREVENT_ACCESSING_MISSING_ATTRIBUTES',
        false
    ),

    /**
     * Logs a warning if a command runs longer than the specified threshold.
     */
    'log_long_running_command' => env(
        'LOG_LONG_RUNNING_COMMAND',
        true
    ),

    /**
     * Threshold for long running command in milliseconds [ms].
     */
    'log_long_running_command_threshold' => env(
        'LOG_LONG_RUNNING_COMMAND_THRESHOLD',
        5000 // [ms]
    ),

    /**
     * Logs a warning if a HTTP request runs longer than the specified threshold.
     */
    'log_long_running_request' => env(
        'LOG_LONG_RUNNING_REQUEST',
        true
    ),

    /**
     * Threshold for long running HTTP request in milliseconds [ms].
     */
    'log_long_running_request_threshold' => env(
        'LOG_LONG_RUNNING_REQUEST_THRESHOLD',
        5000 // [ms]
    ),

    /**
     * Logs a warning if a DB connection runs longer than the specified threshold.
     */
    'log_long_running_total_db_query' => env(
        'LOG_LONG_RUNNING_TOTAL_DB_QUERY',
        true
    ),

    /**
     * Threshold for long running db connection in milliseconds [ms].
     */
    'log_long_running_total_db_query_threshold' => env(
        'LOG_LONG_RUNNING_TOTAL_DB_QUERY_THRESHOLD',
        2000 // [ms]
    ),

    /**
     * Logs a warning if a single DB Query runs longer than the specified threshold.
     */
    'log_long_running_single_db_query' => env(
        'LOG_LONG_RUNNING_SINGLE_DB_QUERY',
        true
    ),

    /**
     * Threshold for long running single DB Query in milliseconds [ms].
     */
    'log_long_running_single_db_query_threshold' => env(
        'LOG_LONG_RUNNING_SINGLE_DB_QUERY_THRESHOLD',
        1000 // [ms]
    ),

    /**
     * Logs a warning if a request cycle consumed more memory than the specified threshold.
     */
    'log_memory_heap_size' => env(
        'LOG_MEMORY_HEAP_SIZE',
        true
    ),

    /**
     * Threshold for memory heap size in Megabytes [MB].
     */
    'log_memory_heap_size_threshold' => env(
        'LOG_MEMORY_HEAP_SIZE_THRESHOLD',
        50 // [MB]
    ),

];
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Acknowledgements
----------------

[](#acknowledgements)

This package is based on the following article:

- [Laravel's safety mechanisms](https://planetscale.com/blog/laravels-safety-mechanisms#model-strictness) by [Aaron Francis](https://github.com/aarondfrancis)

Credits
-------

[](#credits)

- [Kai Niklas](https://github.com/kainiklas)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 68.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~33 days

Recently: every ~42 days

Total

6

Last Release

1138d ago

Major Versions

0.1.1 → 1.0.02022-10-26

1.0.2 → 2.0.02023-04-06

### Community

Maintainers

![](https://www.gravatar.com/avatar/aeb8a1e9fda3eb3ceb23ddd1cdd17dce0f4a710c794c17848758c5e60b330699?d=identicon)[kainiklas](/maintainers/kainiklas)

---

Top Contributors

[![kainiklas](https://avatars.githubusercontent.com/u/9416642?v=4)](https://github.com/kainiklas "kainiklas (31 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (8 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (6 commits)")

---

Tags

laravelphplaravelkainiklaslaravel-strict-mode

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/kainiklas-laravel-strict-mode/health.svg)

```
[![Health](https://phpackages.com/badges/kainiklas-laravel-strict-mode/health.svg)](https://phpackages.com/packages/kainiklas-laravel-strict-mode)
```

###  Alternatives

[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spatie/laravel-model-flags

Add flags to Eloquent models

4301.1M1](/packages/spatie-laravel-model-flags)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[spatie/laravel-sql-commenter

Add comments to SQL queries made by Laravel

1931.4M1](/packages/spatie-laravel-sql-commenter)[spatie/laravel-deleted-models

Automatically copy deleted records to a separate table

409109.8k4](/packages/spatie-laravel-deleted-models)[wnx/laravel-backup-restore

A package to restore database backups made with spatie/laravel-backup.

203330.1k2](/packages/wnx-laravel-backup-restore)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
