You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/docs/database/files.md
+58-19Lines changed: 58 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ Leaf MVC has always taken cues from Laravel and Rails to make database managemen
4
4
5
5
## What are Schema Files?
6
6
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.
8
8
9
9
```yml [flights.yml]
10
10
columns:
@@ -14,10 +14,6 @@ columns:
14
14
15
15
seeds:
16
16
count: 10
17
-
data:
18
-
to: '@faker.city'
19
-
from: '@faker.city'
20
-
identifier: '@faker.uuid'
21
17
```
22
18
23
19
## Creating a Schema File
@@ -37,7 +33,7 @@ php leaf g:schema posts
37
33
This will create a schema file at `app/database/posts.yml` which looks like this:
38
34
39
35
```yml [posts.yml]
40
-
# schema files add auto-increments and timestamps by default
36
+
# schema files add auto-increments and timestamps automatically
41
37
42
38
# you can add all the columns you want under the columns key
43
39
columns:
@@ -53,14 +49,10 @@ columns:
53
49
relationships:
54
50
- User
55
51
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
57
53
seeds:
58
54
count: 5
59
55
truncate: true
60
-
data:
61
-
name: '@faker.name'
62
-
identifier: '@faker.uuid'
63
-
verified_at: '@tick.format:YYYY-MM-DD HH:mm:ss'
64
56
```
65
57
66
58
Breaking this file down, there are three main sections:
@@ -69,7 +61,7 @@ Breaking this file down, there are three main sections:
69
61
70
62
- `seeds`: This is used to set the seeders of the table. The available properties are:
71
63
- `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. -->
73
65
- `truncate`: This is used to truncate the table before seeding.
74
66
75
67
- `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
395
387
396
388
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.
397
389
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:
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.
After creating your seeder, you can run your seeders using the `db:seed` command:
@@ -429,6 +421,8 @@ php leaf db:seed
429
421
430
422
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.
431
423
424
+
## Using factories for generating fake data
425
+
432
426
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:
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.
444
438
445
439
After adding your seeds, you can run your seeders using the `db:seed` command:
446
440
@@ -454,7 +448,52 @@ If you want to seed a specific table, you can pass the table name as an argument
454
448
php leaf db:seed users
455
449
```
456
450
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:
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
458
497
459
498
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.
0 commit comments