PHPackages                             hedronium/seed-cascade - 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. hedronium/seed-cascade

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

hedronium/seed-cascade
======================

A range based Cascading Seeder for Laravel.

v1.1.0(9y ago)3344[3 issues](https://github.com/Hedronium/SeedCascade/issues)MITPHP

Since Jul 9Pushed 9y ago3 watchersCompare

[ Source](https://github.com/Hedronium/SeedCascade)[ Packagist](https://packagist.org/packages/hedronium/seed-cascade)[ Docs](http://hedronium.github.io/SeedCascade/)[ RSS](/packages/hedronium-seed-cascade/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

SeedCascade
===========

[](#seedcascade)

> A range based cascading seeder for Laravel.

This package allows you to define your database seeds by specifying ranges of numbers like `1-10` in a cascading manner.

Documentation
-------------

[](#documentation)

View the [Documentation](http://hedronium.github.io/SeedCascade/);

Introduction
------------

[](#introduction)

Heres what your new Seeder classes would look like.

```
use Hedronium\SeedCascade\SeedCascade;

class DatabaseSeeder extends SeedCascade {
  public $table = "food";

  public function seedSheet() {
    return [
      '1-6' => [
        'name' => 'Cabbage',
        'type' => 'vegetable'
      ],
      '4-6' => [
        'name' => 'Carrot'
      ]
    ];
  }
}
```

Inserted Data in `food` table:

nametypeCabbagevegetableCabbagevegetableCabbagevegetableCarrotvegetableCarrotvegetableCarrotvegetableIt inserts 6 rows into the `food` table, with the name column being set to "Cabbage" on the first three and "Carrot" on the last three.

### Features List

[](#features-list)

- Range Based Seeding
- Overlapping Ranges
- Interting data directly into database
- Using Models to Insert Data
- Field Inheritance
- String Interpolation
- Closures as value source
- Methods as value source

Getting Started
---------------

[](#getting-started)

### Installation

[](#installation)

Install it via composer. Run the following command in your [Laravel](//laravel.com) project directory.

```
$ composer require hedronium/seed-cascade
```

View it on [Packagist](//packagist.org/packages/hedronium/seed-cascade)for version information.

### Creating a Seeder

[](#creating-a-seeder)

1. Create a class that extends
    `Hedronium\SeedCascade\SeedCascade`
2. Add the `$table` or `$model` public property.
3. Implement a `seedSheet()` method that returns your Seeding definitions.

Example

```
use Hedronium\SeedCascade\SeedCascade;

class DatabaseSeeder extends SeedCascade {
  public $table = "fruits";

  public function seedSheet() {
    return [
      '1-10' => [
        'name' => 'Potao'
      ]
    ];
  }
}
```

Seeding definitions are contained in an associative array. The keys act as the ranges of the seeding data (eg. `"1-10"` means the first 10 rows inserted), pointing to another associative array consisting of key value pairs.

#### Using a Model

[](#using-a-model)

```
use Hedronium\SeedCascade\SeedCascade;
use App\Potato;

class DatabaseSeeder extends SeedCascade {
  public $model = Potato::class;

  public function seedSheet() {
    // Return seeding definitions.
  }
}
```

#### Multiple Ranges

[](#multiple-ranges)

Multiple ranges can also be specified.

```
return [
    '1-10' => ['name' => 'Potato'],
    '11-20' => ['name' => 'Tomato']
]
```

#### Overlapping Ranges

[](#overlapping-ranges)

Overlapping ranges are not only possible but encouraged!

```
return [
    '1-10' => ['name' => 'Potato', color => 'yellow'],
    '6-10' => ['name' => 'Mango']
]
```

In the case of overlapping ranges, properties are automatically inherited. Meaning all rows from **1** *to* **10** will have the property `color` set to `yellow`.

### Running Seeders

[](#running-seeders)

Its the same as with normal [Laravel Seeders](//laravel.com/docs/5.2/seeding).

```
$ php artisan db:seed
```

See, no surprise here.

Seed Definitions
----------------

[](#seed-definitions)

SeedCascade is so much cooler than what you saw above. Lets get started with leveraging it to its true potential.

### Ranges

[](#ranges)

#### From `x` to `y`

[](#from-x-to-y)

Exactly like above. Two numbers separated by a hyphen.

```
return [
    '1-5' => ['name' => 'Potatoes']
]
```

#### From `x` and onwards

[](#from-x-and-onwards)

This kind of ranges allow you to write definition that apply from a certain point onwards till the end of time.

```
return [
    '3-n' => ['name' => 'Potatoes']
]
```

A number and the character `n` separated by a hyphen.

> Actually the stuff after the hyphen doesn't really matter. Meaning, `'3-Infinity'`, `'3-n'`, `'3-potato'` or even `'3-'` do exactly the same thing.`

**Wanring:** If we use the 'to infinity' ranges such as above, we absolutely MUST set the `$count` property on the Seeder class, else SeedCascade would be very mad!

```
class DatabaseSeeder extends SeedCascade
{
    public $count = 20;
    public $table = "edible_things";

    public function seedSheet()
    {
        return [
            '1-10' => ['name' => 'Potao'],
            '3-n' => ['name' => 'Tomato']
        ];
    }
}
```

This makes sure the seeder knows when to stop.

#### Only `x`

[](#only-x)

Sometimes we might want to change the value of a single row. For that we may just make a key without defining a range.

```
return [
    '1-10' => ['name' => 'Mango'],
    '3' => ['name' => 'Dragon Fruit']
]
```

Only the 3rd row inserted will have its `name` field set with `'Dragon Fruit'`.

### Values

[](#values)

#### Direct Value

[](#direct-value)

This ones simple, we just type in the value.

```
return [
    '1-n' => ['name' => 'Potato', 'price' => 20],
    '5-n' => ['name' => 'Potato', 'price' => 100]
]
```

#### Array

[](#array)

This ones pretty simple too. We type is multiple values as an array.

```
return [
  '1-4' => [
	'type' => 'fruit',
	'name' => $this->arr([
		'tomato',
		'mango',
		'lichee',
		'pineapple'
	])
  ]
]
```

If the range is larger than the number of array elements (like `1-6`), then `null` will be returned.

There is also an optional second parameter. It's called `$wrap`. If you set it to `true`, the seeder will just start from the beginning again.

```
return [
  '1-5' => [
	'type' => 'fruit',
	'name' => $this->arr([
		'fruit X',
		'fruit Y'
	], true)
  ]
]
```

will result in

nametypefruit Xfruitfruit Yfruitfruit Xfruitfruit Yfruitfruit Xfruit#### String Interpolation

[](#string-interpolation)

#### Inheritance

[](#inheritance)

We could ofcourse just write an average string and go your merry way but we threw in a few extra magic just incase. Lets start with an example:

```
return [
  '1-10' => ['name' => 'Potato', 'price' => 30],
  '5-10' => ['name' => 'Super {inherit}', 'price' => 100]
]
```

`{inherit}` will be replaced with `"Potato"`. It means all the rows 6 from **5** *to* **10** will be populated with `Super Potato` in their `name` fields.

#### Referencing a Field

[](#referencing-a-field)

What if we want to interpolate in another field within the row? SeedCascade has got us covered!

```
return [
    '1-10' => [
        'name' => '{self.type} Engine ({self.model})',
        'type' => 'Jet',
        'model' => 'C3'
    ]
];
```

This will insert 10 rows into the database with all their name fields being `Jet engine (C3)`.

This feature really shines when the current definition block doesn't really have the certain property being refferenced. Eg:

```
return [
    '1-10' => [
        'type' => 'Jet'
    ],
    '1-5' => [
        'name' => '{self.type} Fuel'
    ],
    '6-10' => [
        'name' => '{self.type} Engine'
    ]
];
```

Remmeber the "Cascade" part? So now, we'll have 10 rows in the database. the first 5 being `Jet Fuel` and the last five being `Jet Engine` and all of them will have a `type` of `Jet`.

#### Explicitly Inherit a field

[](#explicitly-inherit-a-field)

Wanna make sure you get the value from a larger raneg higher up? Have a look:

```
return [
  '1-10' => ['color' => 'Red'],
  '6-10' => [
    'name' => '{inherit.color} Flower',
    'color' => 'Yellow'
  ]
]
```

the last 5 rows will have a name of `'Red Flower'` **NOT** *`'Yellow Flower'`*

#### Iteration Count

[](#iteration-count)

You can also use `{i}` that has the current iteration count.

```
return [
    '1-5' => ['username' => 'user_{i}']
]
```

This will generate 5 rows with username set to: `user_1`, `user_2`, `user_3`, `user_4`, `user_5` respectively.

**Note:** Iteration count starts from `1`.

### Closures

[](#closures)

Sometimes you need more power! *Maybe because you're greedy?* jk jk lol. Values can be closures too.

```
return [
    '1-10' => [
        'order' => function () {
            return rand(1, 100);
        }
    ]
];
```

This will insert 10 rows with the value for `order` being a random integer returned by the closure we provided.

#### Parameters

[](#parameters)

Closures recieve 3 parameters.

- `$i` - The Iteration Count.
- `$self` - An object taht lets you access other field values.
- `$inherit` - An objects that allows you to inherit field values.

#### $i

[](#i)

The use of `$i` is obvious. Heres the username example with closures:

```
return [
    '1-5' => [
        'username' => function ($i) {
            return 'user_'.$i;
        }
    ]
];
```

#### $self

[](#self)

The `$self` object has magic methods implemented that allows dynamic refferencing of properties.

```
return [
    '1-3' => [
        'type' => 'admin'
        'username' => function ($i, $self) {
            return $self->type . '_' . $i;
        }
    ]
];
```

this will insert 3 rows with username being `admin_1`, `admin_2`, `admin_3`.

#### $inherit

[](#inherit)

The $inherit object can both be invoked like `$inherit()` or concatenated or interpolated into a string like `'super_'.$inherit` or `"super_$inherit"` and all will result in the same value.

```
return [
    '1-n' => [
        'price' => 10
    ],
    '6-10' => [
        'price' => function ($i, $self, $inherit) {
            return $inherit() * 2;
        }
    ]
];
```

the last 5 rows will have twice the price as the first 5 rows.
string on the other hand may directly be mixed in like:

```
return [
    '1-n' => [
        'type' => 'admin'
    ],
    '6-10' => [
        'type' => function ($i, $self, $inherit) {
            return "super_$inherit";
            // or
            return "super_".$inherit;
        }
    ]
];
```

Inheriting other fields is also supported:

```
return [
  '1-n' => [
    'username' => 'user_{i}'
  ],
  '6-10' => [
    'hash' => function ($i, $self, $inherit) {
      return md5($inherit->username);
    }
  ]
];
```

#### Bound Closures

[](#bound-closures)

Want closures that refference the seeder class' properties or called a seeder class method? Checkout the `local(Closure $closure)` method.

```
class DatabaseSeeder extends SeedCascade
{
  public $table = "magic";
  protected $meaning_of_life = 42;

  public function seedSheet()
  {
    return [
      '1-10' => [
        'lucky_number' => $this->local(function () {
          return $this->meaning_of_life * rand(1,10)
        })
      ]
    ];
  }
}
```

YES. The `$meaning_of_life` is available inside the closure. MAGIC. Local closures also passed the same parameters as average closures. `$i`, `$self` &amp; `$inherit`.

#### Using Methods

[](#using-methods)

Let's convert the above example to use a class method instead.

```
class DatabaseSeeder extends SeedCascade
{
  public $table = "magic";
  protected $meaning_of_life = 42;

  protected function life() {
    return $this->meaning_of_life * rand(1, 10);
  }

  public function seedSheet()
  {
    return [
      '1-10' => [
        'lucky_number' => $this->method('life')
      ]
    ];
  }
}
```

### Constructors &amp; Faker

[](#constructors--faker)

SeedCascade doesn't mind you defining constructors (or destructors). Heres an example that uses Faker:

```
class DatabaseSeeder extends SeedCascade
{
    public $table = "people";
    protected $faker = null;

    public function __construct()
    {
        $this->faker = new Faker;
    }

    public function seedSheet()
    {
        return [
            '1-10' => [
                'name' => $this->local(function () {
                    return $this->faker->name;
                })
            ]
        ];
    }
}
```

Order of Evaluation
-------------------

[](#order-of-evaluation)

SeedCascade will start evaluating with the most specific range. Meaning `1-10` will be evaluated first before `1-20` or `1-Infinity`.

Inheritance will also depend on this. If you `{inherit}` inside the range `1-10` the value interpolated will be resolved from the range `1-20`. If the super range does not have the property it will just traverse backwards till it find a larger range that defines the property.

If no higher ranges define the property `{inherit}` will become `null`.

###  Health Score

30

—

LowBetter than 61% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~139 days

Total

2

Last Release

3549d ago

### Community

Maintainers

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

---

Top Contributors

[![omranjamal](https://avatars.githubusercontent.com/u/4700757?v=4)](https://github.com/omranjamal "omranjamal (21 commits)")

---

Tags

laravel-5-packagelaravelseedingseederrangecascading

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hedronium-seed-cascade/health.svg)

```
[![Health](https://phpackages.com/badges/hedronium-seed-cascade/health.svg)](https://phpackages.com/packages/hedronium-seed-cascade)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.9M109](/packages/mongodb-laravel-mongodb)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k35.7M51](/packages/kirschbaum-development-eloquent-power-joins)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8723.3M27](/packages/yajra-laravel-oci8)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.6M2](/packages/glushkovds-phpclickhouse-laravel)

PHPackages © 2026

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