Installation:
composer require nunomaduro/essentials
Publish the config (optional but recommended for customization):
php artisan vendor:publish --provider="NunoMaduro\Essentials\EssentialsServiceProvider" --tag="config"
First Use Case:
use \NunoMaduro\Essentials\Strict\Concerns\StrictModel; and add $guarded = []; to enforce strict property access.use \NunoMaduro\Essentials\Immutable\Concerns\ImmutableDate; to make dates immutable in your models.Where to Look First:
config/essentials.php (for customizing behavior like strictness levels, default eager loads, etc.).StrictModel, ImmutableDate, and AutoEagerLoad traits in vendor/nunomaduro/essentials/src/Strict/Concerns and src/Immutable/Concerns.php artisan essentials:install to scaffold common files (e.g., .env.example, routes/web.php, routes/api.php).Strict Models:
StrictModel in your Eloquent models to enforce strict property access.
use NunoMaduro\Essentials\Strict\Concerns\StrictModel;
class User extends Model
{
use StrictModel;
protected $guarded = []; // Only allow mass assignment for these fields
}
Auto-Eager Loading:
$autoEagerLoad in your models to automatically eager load relationships.
class User extends Model
{
use \NunoMaduro\Essentials\AutoEagerLoad\Concerns\AutoEagerLoad;
protected $autoEagerLoad = ['posts', 'comments']; // Relationships to always eager load
}
with() in queries to override defaults:
User::with(['posts' => function ($query) {
$query->where('published', true);
}])->get();
Immutable Dates:
ImmutableDate trait to models to ensure dates cannot be modified after creation.
use NunoMaduro\Essentials\Immutable\Concerns\ImmutableDate;
class Post extends Model
{
use ImmutableDate;
protected $dates = ['published_at'];
}
published_at will throw exceptions if modified after initialization.Artisan Commands:
php artisan essentials:install: Scaffold boilerplate files.php artisan essentials:strict:check: Validate models for strict compliance.php artisan essentials:immutable:check: Validate immutable date usage.Testing:
EssentialsServiceProvider in tests to isolate behavior:
$this->app->register(\NunoMaduro\Essentials\EssentialsServiceProvider::class);
essentials:install command to standardize new projects.AutoEagerLoad with apiResources to optimize response times.StrictModel to catch invalid column names early via $fillable/$guarded mismatches.Strict Models:
$guarded or $fillable will throw MassAssignmentException. Always validate with:
php artisan essentials:strict:check
$model->dynamicProp = 'value') unless explicitly allowed in $guarded.Auto-Eager Loading:
DB::enableQueryLog().with() to limit depth.Immutable Dates:
$post->published_at = now()) throws ImmutableDateException. Use setters or factory methods instead:
$post->setPublishedAt(now());
public function getPublishedAtAttribute($value) {
return $value->toDateTimeString();
}
Configuration:
strict config key in essentials.php defaults to true. Set to false to disable strict checks globally (not recommended for production).max_auto_eager_load_depth config prevents stack overflows in deeply nested relationships.Testing:
$model = $this->partialMock(User::class, ['setAttribute']);
$model->shouldReceive('setAttribute')->andThrow(ImmutableDateException::class);
debugbar to inspect mass assignment payloads.DB::listen() to log generated queries:
DB::listen(function ($query) {
\Log::debug($query->sql, $query->bindings);
});
ImmutableDateException in logs or use a try-catch block to handle gracefully:
try {
$post->published_at = now();
} catch (ImmutableDateException $e) {
\Log::warning('Attempted to modify immutable date', ['model' => $post]);
}
Custom Traits:
StrictModel) to add domain-specific validation:
trait CustomStrictModel extends StrictModel {
public function setAttribute($key, $value) {
if ($key === 'email' && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException('Invalid email format.');
}
parent::setAttribute($key, $value);
}
}
Dynamic Eager Loading:
getAutoEagerLoad() in models for dynamic behavior:
public function getAutoEagerLoad()
{
return request()->user()->isAdmin() ? ['posts', 'users'] : ['posts'];
}
Immutable Attributes:
ImmutableDate to support other immutable fields (e.g., UUIDs):
use NunoMaduro\Essentials\Immutable\Concerns\ImmutableAttribute;
trait ImmutableUuid {
use ImmutableAttribute;
protected $immutableAttributes = ['uuid'];
}
Artisan Commands:
EssentialsServiceProvider:
$this->commands([
\App\Console\Commands\CustomEssentialsCommand::class,
]);
How can I help you explore Laravel packages today?