Download the PHP package hedronium/seed-cascade without Composer
On this page you can find all versions of the php package hedronium/seed-cascade. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package seed-cascade
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
View the Documentation;
Introduction
Heres what your new Seeder classes would look like.
Inserted Data in food
table:
name | type |
---|---|
Cabbage | vegetable |
Cabbage | vegetable |
Cabbage | vegetable |
Carrot | vegetable |
Carrot | vegetable |
Carrot | vegetable |
It 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
- 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
Installation
Install it via composer. Run the following command in your Laravel project directory.
View it on Packagist for version information.
Creating a Seeder
-
Create a class that extends
Hedronium\SeedCascade\SeedCascade
- Add the
$table
or$model
public property. - Implement a
seedSheet()
method that returns your Seeding definitions.
Example
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
Multiple Ranges
Multiple ranges can also be specified.
Overlapping Ranges
Overlapping ranges are not only possible but encouraged!
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
Its the same as with normal Laravel Seeders.
See, no surprise here.
Seed Definitions
SeedCascade is so much cooler than what you saw above. Lets get started with leveraging it to its true potential.
Ranges
From x
to y
Exactly like above. Two numbers separated by a hyphen.
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.
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!
This makes sure the seeder knows when to stop.
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.
Only the 3rd row inserted will have its name
field set with 'Dragon Fruit'
.
Values
Direct Value
This ones simple, we just type in the value.
Array
This ones pretty simple too. We type is multiple values as an array.
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.
will result in
name | type |
---|---|
fruit X | fruit |
fruit Y | fruit |
fruit X | fruit |
fruit Y | fruit |
fruit X | fruit |
String Interpolation
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:
{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
What if we want to interpolate in another field within the row? SeedCascade has got us covered!
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:
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
Wanna make sure you get the value from a larger raneg higher up? Have a look:
the last 5 rows will have a name of 'Red Flower'
NOT 'Yellow Flower'
Iteration Count
You can also use {i}
that has the current iteration count.
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
Sometimes you need more power! Maybe because you're greedy? jk jk lol. Values can be closures too.
This will insert 10 rows with the value for order
being a random integer returned by the closure we provided.
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
The use of $i
is obvious. Heres the username example with closures:
$self
The $self
object has magic methods implemented that allows dynamic refferencing of properties.
this will insert 3 rows with username being admin_1
, admin_2
, admin_3
.
$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.
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:
Inheriting other fields is also supported:
Bound Closures
Want closures that refference the seeder class' properties or called a seeder class method? Checkout the local(Closure $closure)
method.
YES. The $meaning_of_life
is available inside the closure. MAGIC. Local closures also passed the same parameters as average closures. $i
, $self
& $inherit
.
Using Methods
Let's convert the above example to use a class method instead.
Constructors & Faker
SeedCascade doesn't mind you defining constructors (or destructors). Heres an example that uses Faker:
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
.