Download the PHP package dayemsiddiqui/eloquent-defaults without Composer
On this page you can find all versions of the php package dayemsiddiqui/eloquent-defaults. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dayemsiddiqui/eloquent-defaults
More information about dayemsiddiqui/eloquent-defaults
Files in dayemsiddiqui/eloquent-defaults
Package eloquent-defaults
Short Description An elegant way to automatically insert default rows to a table whenever a new row to a specific model is created
License MIT
Homepage https://github.com/dayemsiddiqui/eloquent-defaults
Informations about the package eloquent-defaults
Eloquent Defaults
An elegant Laravel package that enables Eloquent models to automatically seed default rows when a related "root" model is created. Perfect for multi-tenant applications, SaaS products, and any scenario where you need to populate related data automatically.
The Problem
In Laravel applications, it's common to need pre-filled models with default data whenever a new parent/root model is created. This could include default plans for a company, default permissions for a role, or default tasks for a project. Most developers handle this with events, service classes, or observers — leading to boilerplate, poor discoverability, and scattered logic.
The Solution
Eloquent Defaults provides a clean, declarative way to define what default data should be seeded when a root model is created. With a single trait and a couple of lines, your models can automatically create their default related records.
Installation
You can install the package via composer:
Quick Example
Now whenever a Company
is created, three default plans will automatically be seeded!
Features
- Type Safe: Full IDE support with typed parameters and generics
- Model-Centric: Each model defines its own defaults using familiar Laravel patterns
- Uses Model::make(): Better control flow and validation than raw arrays
- Automatic: No manual event binding or service provider registration required
- Multi-Model Support: Multiple models can create defaults for the same trigger
- Zero Configuration: Works out of the box with no config files
- Transaction Safe: All default creation happens within database transactions
- Laravel 10-12 Compatible: Built for modern Laravel applications
Usage
Basic Setup
- Add the
HasEloquentDefaults
trait to your model - Implement the
eloquentDefaults()
method with a typed parameter - Return an array of models created with
::make()
Multiple Models for One Trigger
You can have multiple models create defaults for the same trigger model:
Advanced Examples
Dynamic Defaults Based on Model Data
Complex Relationships
Debugging
View all registered model relationships:
Testing
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.
Credits
- Dayem Siddiqui
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of eloquent-defaults with dependencies
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^10.0||^11.0||^12.0