Skip to content

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

License

Notifications You must be signed in to change notification settings

kainiklas/laravel-strict-mode

Repository files navigation

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

Laravel Strict Mode

Latest Version on Packagist GitHub Tests Action Status Total Downloads

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

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

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Acknowledgements

This package is based on the following article:

Credits

License

The MIT License (MIT). Please see License File for more information.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Languages