PHPackages                             dayemsiddiqui/eloquent-defaults - 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. dayemsiddiqui/eloquent-defaults

ActiveLibrary

dayemsiddiqui/eloquent-defaults
===============================

An elegant way to automatically insert default rows to a table whenever a new row to a specific model is created

v2.1.0(10mo ago)0338[4 PRs](https://github.com/dayemsiddiqui/eloquent-defaults/pulls)MITPHPPHP ^8.4CI passing

Since Jul 14Pushed 1mo agoCompare

[ Source](https://github.com/dayemsiddiqui/eloquent-defaults)[ Packagist](https://packagist.org/packages/dayemsiddiqui/eloquent-defaults)[ Docs](https://github.com/dayemsiddiqui/eloquent-defaults)[ GitHub Sponsors]()[ RSS](/packages/dayemsiddiqui-eloquent-defaults/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (12)Versions (9)Used By (0)

Eloquent Defaults
=================

[](#eloquent-defaults)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b9904bdbbc5ae068346b4f6711c00c4903783b8f5e2dd9bbb79eeb815b993811/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646179656d73696464697175692f656c6f7175656e742d64656661756c74732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dayemsiddiqui/eloquent-defaults)[![GitHub Tests Action Status](https://camo.githubusercontent.com/a57436d5d323ad899ab824c12cef7662f7282bc2a57f23dbeae4868360705a8f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f646179656d73696464697175692f656c6f7175656e742d64656661756c74732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/dayemsiddiqui/eloquent-defaults/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/68b0551892c352be620b06e3e9adebaf5b6d205ae09e9e7b5e4831a31e7584f4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f646179656d73696464697175692f656c6f7175656e742d64656661756c74732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/dayemsiddiqui/eloquent-defaults/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/6a8567782f486758b01709377ca5c4a5c7062a83ca0db50b8b0f3d3579a5aac7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646179656d73696464697175692f656c6f7175656e742d64656661756c74732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dayemsiddiqui/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
-----------

[](#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
------------

[](#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
------------

[](#installation)

You can install the package via composer:

```
composer require dayemsiddiqui/eloquent-defaults
```

Quick Example
-------------

[](#quick-example)

```
use Illuminate\Database\Eloquent\Model;
use dayemsiddiqui\EloquentDefaults\Traits\HasEloquentDefaults;

class Plan extends Model
{
    use HasEloquentDefaults;

    protected $fillable = ['company_id', 'name', 'price'];

    protected static function eloquentDefaults(Company $company): array
    {
        return [
            Plan::make(['company_id' => $company->id, 'name' => 'Basic Plan', 'price' => 999]),
            Plan::make(['company_id' => $company->id, 'name' => 'Pro Plan', 'price' => 1999]),
            Plan::make(['company_id' => $company->id, 'name' => 'Enterprise Plan', 'price' => 4999]),
        ];
    }
}
```

Now whenever a `Company` is created, three default plans will automatically be seeded!

```
$company = Company::create(['name' => 'Acme Corp']);
// Automatically creates 3 plans for this company
```

Features
--------

[](#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
-----

[](#usage)

### Basic Setup

[](#basic-setup)

1. Add the `HasEloquentDefaults` trait to your model
2. Implement the `eloquentDefaults()` method with a typed parameter
3. Return an array of models created with `::make()`

```
use dayemsiddiqui\EloquentDefaults\Traits\HasEloquentDefaults;

class Role extends Model
{
    use HasEloquentDefaults;

    protected $fillable = ['company_id', 'name', 'permissions'];

    protected static function eloquentDefaults(Company $company): array
    {
        return [
            Role::make(['company_id' => $company->id, 'name' => 'Admin', 'permissions' => 'all']),
            Role::make(['company_id' => $company->id, 'name' => 'Editor', 'permissions' => 'read,write']),
            Role::make(['company_id' => $company->id, 'name' => 'Viewer', 'permissions' => 'read']),
        ];
    }
}
```

### Multiple Models for One Trigger

[](#multiple-models-for-one-trigger)

You can have multiple models create defaults for the same trigger model:

```
// All of these will create defaults when a Company is created
class Plan extends Model {
    use HasEloquentDefaults;
    protected static function eloquentDefaults(Company $company): array { /* ... */ }
}
class Role extends Model {
    use HasEloquentDefaults;
    protected static function eloquentDefaults(Company $company): array { /* ... */ }
}
class Setting extends Model {
    use HasEloquentDefaults;
    protected static function eloquentDefaults(Company $company): array { /* ... */ }
}
```

### Advanced Examples

[](#advanced-examples)

#### Dynamic Defaults Based on Model Data

[](#dynamic-defaults-based-on-model-data)

```
class Subscription extends Model
{
    use HasEloquentDefaults;

    protected static function eloquentDefaults(User $user): array
    {
        $defaults = [
            Subscription::make(['user_id' => $user->id, 'plan' => 'free']),
        ];

        // Add bonus subscription for verified users
        if ($user->email_verified_at) {
            $defaults[] = Subscription::make([
                'user_id' => $user->id,
                'plan' => 'verified_bonus',
                'expires_at' => now()->addDays(30)
            ]);
        }

        return $defaults;
    }
}
```

#### Complex Relationships

[](#complex-relationships)

```
class Profile extends Model
{
    use HasEloquentDefaults;

    protected static function eloquentDefaults(User $user): array
    {
        return [
            Profile::make([
                'user_id' => $user->id,
                'bio' => "Welcome {$user->name}! Thanks for joining our platform.",
                'avatar' => 'default-avatar.png',
                'preferences' => json_encode([
                    'theme' => 'light',
                    'notifications' => true,
                    'locale' => 'en'
                ])
            ]),
        ];
    }
}
```

### Debugging

[](#debugging)

View all registered model relationships:

```
use dayemsiddiqui\EloquentDefaults\Facades\EloquentDefaults;

EloquentDefaults::debugRegistrations();
// Returns array of all target models and their provider models
```

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.

Credits
-------

[](#credits)

- [Dayem Siddiqui](https://github.com/dayemsiddiqui)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance75

Regular maintenance activity

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~0 days

Total

4

Last Release

301d ago

Major Versions

v1.1.0 → v2.0.02025-07-14

### Community

Maintainers

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

---

Top Contributors

[![dayemsiddiqui](https://avatars.githubusercontent.com/u/9693176?v=4)](https://github.com/dayemsiddiqui "dayemsiddiqui (3 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (2 commits)")

---

Tags

laravelDayem Siddiquieloquent-defaults

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/dayemsiddiqui-eloquent-defaults/health.svg)

```
[![Health](https://phpackages.com/badges/dayemsiddiqui-eloquent-defaults/health.svg)](https://phpackages.com/packages/dayemsiddiqui-eloquent-defaults)
```

###  Alternatives

[vormkracht10/laravel-mails

Laravel Mails can collect everything you might want to track about the mails that has been sent by your Laravel app.

24149.7k](/packages/vormkracht10-laravel-mails)[spatie/laravel-prometheus

Export Laravel metrics to Prometheus

2651.3M6](/packages/spatie-laravel-prometheus)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)[scalar/laravel

Render your OpenAPI-based API reference

6183.9k2](/packages/scalar-laravel)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)[musahmusah/laravel-multipayment-gateways

A Laravel Package that makes implementation of multiple payment Gateways endpoints and webhooks seamless

852.2k1](/packages/musahmusah-laravel-multipayment-gateways)

PHPackages © 2026

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