Skip to content

Commit 1f82565

Browse files
committed
feat: add docs for new schema seeds
1 parent 3099f21 commit 1f82565

File tree

1 file changed

+58
-19
lines changed

1 file changed

+58
-19
lines changed

src/docs/database/files.md

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Leaf MVC has always taken cues from Laravel and Rails to make database managemen
44

55
## What are Schema Files?
66

7-
With Schema Files in Leaf MVC 4, you can define your database tables, seed data, and test data—all in one simple YAML file. No need to manage separate migrations, seeders, or factories. It’s clean, readable, and designed to help you move fast without the overhead.
7+
Schema Files allow you to define your database tables, seed data, and keep track of your database automatically in one simple YAML file. It’s clean, readable, and designed to help you move fast without the overhead.
88

99
```yml [flights.yml]
1010
columns:
@@ -14,10 +14,6 @@ columns:
1414

1515
seeds:
1616
count: 10
17-
data:
18-
to: '@faker.city'
19-
from: '@faker.city'
20-
identifier: '@faker.uuid'
2117
```
2218
2319
## Creating a Schema File
@@ -37,7 +33,7 @@ php leaf g:schema posts
3733
This will create a schema file at `app/database/posts.yml` which looks like this:
3834

3935
```yml [posts.yml]
40-
# schema files add auto-increments and timestamps by default
36+
# schema files add auto-increments and timestamps automatically
4137
4238
# you can add all the columns you want under the columns key
4339
columns:
@@ -53,14 +49,10 @@ columns:
5349
relationships:
5450
- User
5551
56-
# seeds are optional and can be used to populate the database with dummy data
52+
# seeds are optional and can be used to populate the database with dummy data from models
5753
seeds:
5854
count: 5
5955
truncate: true
60-
data:
61-
name: '@faker.name'
62-
identifier: '@faker.uuid'
63-
verified_at: '@tick.format:YYYY-MM-DD HH:mm:ss'
6456
```
6557

6658
Breaking this file down, there are three main sections:
@@ -69,7 +61,7 @@ Breaking this file down, there are three main sections:
6961

7062
- `seeds`: This is used to set the seeders of the table. The available properties are:
7163
- `count`: This is used to set the number of seeds to generate.
72-
- `data`: This is used to set the data of the seeds. The key is the column name and the value is the value of the column. You can use `@faker.[value]` to generate fake data for the column. <!-- You can also use `{{ [value] }}` to use PHP code, but this is a separate PHP thread which means you can't use variables from the current scope. -->
64+
<!-- - `data`: This is used to set the data of the seeds. The key is the column name and the value is the value of the column. You can use `@faker.[value]` to generate fake data for the column. You can also use `{{ [value] }}` to use PHP code, but this is a separate PHP thread which means you can't use variables from the current scope. -->
7365
- `truncate`: This is used to truncate the table before seeding.
7466

7567
- `relationships`: This is used to set the relationships of the table. The value is an array of models the table is related to. This is used to generate foreign keys for the table.
@@ -395,17 +387,17 @@ php leaf db:drop users
395387

396388
Database seeds let you pre-populate your database with initial data—whether it's default settings, test data, or sample records. Instead of manually adding entries, you can use seeders to automate this process.
397389

398-
In Leaf MVC, you define seeders directly in your Schema Files under the `seeds` key. This keeps everything in one place, making it easier to manage your database setup. Here's an example of a seeder:
390+
In Leaf MVC, you can define seeders directly in your Schema Files under the `seeds` key. This keeps everything in one place, making it easier to manage your database setup. Here's an example of a seeder:
399391

400392
```yml [users.yml]
401393
seeds:
402394
data:
403395
- name: 'Example User'
404396
405-
password: '@hash:passwordForThisUser' # @hash requires leafs/password
397+
password: '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi'
406398
- name: 'Another User'
407399
408-
password: '@hash:passwordForThisUser' # @hash requires leafs/password
400+
password: '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi'
409401
```
410402

411403
In this example, we create a seeder that seeds the `users` table with two example users. We are passing an array of seeds to the `data` key, each seed being a key value pair of column name and value.
@@ -418,7 +410,7 @@ seeds:
418410
data:
419411
name: 'Example User'
420412
421-
password: '@hash:password'
413+
password: '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi'
422414
```
423415

424416
After creating your seeder, you can run your seeders using the `db:seed` command:
@@ -429,6 +421,8 @@ php leaf db:seed
429421

430422
This will generate 10 seeds for the `users` table with the same data which is not very useful. To generate multiple fake seeds, you can use what other frameworks call a factory.
431423

424+
## Using factories for generating fake data
425+
432426
In Leaf MVC, factories and seeders are the same thing as we believe this confusion is unnecessary. If you want to generate fake data for your seeders, you can add `@faker.[value]` as the value of a column in your seeder. Here's an example:
433427

434428
```yml{4,5} [users.yml]
@@ -437,10 +431,10 @@ seeds:
437431
data:
438432
name: '@faker.name'
439433
email: '@faker.email'
440-
password: '@hash:password'
434+
password: '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi'
441435
```
442436

443-
In this example, we're generating 10 fake seeds for the `users` table.
437+
In this example, we're generating 10 fake records for the `users` table.
444438

445439
After adding your seeds, you can run your seeders using the `db:seed` command:
446440

@@ -454,7 +448,52 @@ If you want to seed a specific table, you can pass the table name as an argument
454448
php leaf db:seed users
455449
```
456450

457-
<!-- ## Database migrations vs data migrations
451+
## Writing custom seeders <Badge>New</Badge>
452+
453+
While schema-based seeders are great for simple data, sometimes you need more control. For complex scenarios—like pulling data from an API, processing files, or setting up intricate relationships—you can create seeders in your models. Your model can have a static `__seeder` method that contains the logic for seeding data. Here's an example:
454+
455+
```php [User.php]
456+
<?php
457+
458+
namespace App\Models;
459+
460+
class User extends Model
461+
{
462+
public static function __seeder()
463+
{
464+
return [
465+
'name' => fake()->name(),
466+
'email' => fake()->email(),
467+
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi'
468+
];
469+
}
470+
}
471+
```
472+
473+
In this case, your schema file's `seeds` section can simply specify the number of records to create:
474+
475+
```yml:no-line-numbers [users.yml]
476+
seeds:
477+
count: 10
478+
```
479+
480+
Schema files will automatically detect this method if the model name matches the table name, eg: `User` model for `users` table, `TestUser` model for `test_users` table, etc. You can also specify a different model name in your schema file using the `model` key:
481+
482+
```yml:no-line-numbers [users.yml]
483+
seeds:
484+
count: 10
485+
model: CustomUserModel
486+
```
487+
488+
Then, when you run the `db:seed` command, Leaf will call the `__seeder` method on your model to generate the seed data.
489+
490+
```bash:no-line-numbers
491+
php leaf db:seed
492+
```
493+
494+
<!--
495+
496+
## Database migrations vs data migrations
458497

459498
Usually, when making substancial changes to your database, you would create a migration file which is usually in charge of modifying the structure of your database. In some situations, you might want to run some kind of data migration which may copy data from one table to another, or run some kind of data manipulation on your recently migrated database. Some frameworks combine these two into one, but in Leaf MVC, we separate these two because we believe they are two different things. While database migrations are common, data migrations are not so common and are usually done manually.
460499

0 commit comments

Comments
 (0)