Skip to content

Commit 431d0fa

Browse files
authored
Merge pull request #108 from leafsphp/staging
Staging
2 parents 05c0451 + 38f7948 commit 431d0fa

File tree

6 files changed

+305
-35
lines changed

6 files changed

+305
-35
lines changed

.vitepress/config/sidebar.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,13 @@ const sidebar = [
151151
// collapsed: true,
152152
items: [
153153
{ text: 'Leaf + MVC', link: '/docs/mvc/' },
154+
{ text: 'Leaf MVC v4', link: '/docs/mvc/mvc4' },
154155
{ text: 'Controllers', link: '/docs/mvc/controllers' },
155156
// { text: 'Views', link: '/docs/frontend/mvc' },
156157
{ text: 'Models', link: '/docs/database/models' },
157158
{ text: 'Migrations', link: '/docs/database/migrations' },
158159
{ text: 'JSON Schema', link: '/docs/database/schema' },
159-
// { text: 'Schema Files', link: '/docs/database/files' },
160+
{ text: 'Schema Files', link: '/docs/database/files' },
160161
{ text: 'Seeders', link: '/docs/database/seeders' },
161162
{ text: 'Factories', link: '/docs/database/factories' },
162163
{ text: 'Writing Commands', link: '/docs/mvc/commands' },
Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
# Schema Files
1+
# Schema Files <Badge type="warning">ALPHA v4+</Badge>
22

3-
Leaf MVC inherited all the teachings of Laravel and Ruby on Rails, including the use of migrations, seeders, and factories which made creating, testing and seeding databases a breeze. It even introduced schema files, allowing you to generate migrations from JSON data. While helpful, this added complexity and clutter to projects. To simplify things, we’re moving away from the Rails/Laravel approach and creating a more streamlined, Leaf-like solution.
3+
Leaf MVC inherited all the teachings of Laravel and Ruby on Rails, including the use of migrations, seeders, and factories which made creating, testing and seeding databases a breeze. While this is great and has been tried and tested over the years, having multiple files for a single database table can be a bit of a hassle. This is why we introduced Schema Files in Leaf MVC v4.
44

55
## What are Schema Files?
66

77
Schema files build on the JSON schema idea we introduced in earlier Leaf MVC versions, but they take things further. Instead of juggling separate files for migrations, seeders, and factories, you can handle everything in one place. They’re written in YAML, so they’re easy to read and work with—no extra hassle, no repeating yourself.
88

99
```yml [flights.yml]
10-
increments: true
11-
timestamps: true
1210
columns:
1311
to: string
1412
from: string
@@ -17,9 +15,9 @@ columns:
1715
seeds:
1816
count: 10
1917
data:
20-
to: 'faker:city'
21-
from: 'faker:city'
22-
identifier: 'faker:uuid'
18+
to: '@faker.city'
19+
from: '@faker.city'
20+
identifier: '@faker.uuid'
2321
```
2422
2523
## Creating a Schema File
@@ -33,36 +31,39 @@ php leaf g:schema <table-name>
3331
Remember, every schema file is tied to a table in your database. When you run the command above, Leaf will create a schema file in your `app/database` directory with the name `<table-name>.yml`. Here’s an example:
3432

3533
```bash:no-line-numbers
36-
php leaf g:schema users
34+
php leaf g:schema posts
3735
```
3836

39-
This will create a schema file at `app/database/users.yml` which looks like this:
37+
This will create a schema file at `app/database/posts.yml` which looks like this:
4038

41-
```yml [users.yml]
42-
increments: true
43-
timestamps: true
39+
```yml [posts.yml]
40+
# schema files add auto-increments and timestamps by default
41+
42+
# you can add all the columns you want under the columns key
4443
columns:
4544
name: string
46-
email:
45+
identifier:
4746
type: string
48-
length: 255
4947
unique: true
50-
password: string
51-
email_verified_at: timestamp
48+
verified_at:
49+
type: timestamp
50+
nullable: true
51+
52+
# you can add foreign ids for other models under the relationships key key
53+
relationships:
54+
- User
5255
56+
# seeds are optional and can be used to populate the database with dummy data
5357
seeds:
54-
count: 10
58+
count: 5
59+
truncate: true
5560
data:
56-
name: 'faker:name'
57-
email: 'faker:email'
58-
password: 'hash:password'
61+
name: '@faker.name'
62+
identifier: '@faker.uuid'
63+
verified_at: '@tick.format:YYYY-MM-DD HH:mm:ss'
5964
```
6065

61-
Breaking down this file, we have:
62-
63-
- `increments`: This is used to set the default id of the table. If set to `true`, the table will have an auto-incrementing id. If set to `false`, the table will not have an id, and you can set your own primary key.
64-
65-
- `timestamps`: This is used to set timestamps on the table. If set to `true`, the table will have `created_at` and `updated_at` columns. If set to `false`, the table will not have timestamps.
66+
Breaking this file down, there are three main sections:
6667

6768
- `columns`: This is used to set the columns of the table. The key is the column name and the value is a key value pair of column properties. The available properties are:
6869
- `type`: The type of the column. This can be `string`, `integer`, `timestamp` or any type supported by Laravel's Eloquent.
@@ -80,9 +81,31 @@ Breaking down this file, we have:
8081

8182
- `seeds`: This is used to set the seeders of the table. The available properties are:
8283
- `count`: This is used to set the number of seeds to generate.
83-
- `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. -->
84+
- `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. -->
8485
- `truncate`: This is used to truncate the table before seeding.
8586

87+
- `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.
88+
89+
Besides these, Schema files also set a lot of defaults for you. For instance, the `id` column is set as the primary key and auto-incrementing by default. Timestamps are also added by default. You can override these defaults by adding the `id` and `timestamps` keys to your schema file. Here's an example:
90+
91+
```yml:no-line-numbers [posts.yml]
92+
increments: false # this will remove the auto-incrementing id column
93+
timestamps: false # this will remove the timestamps columns
94+
```
95+
96+
Once you turn off auto-increments, you can add your own `id` column. Here's an example:
97+
98+
```yml:no-line-numbers [posts.yml]
99+
increments: false
100+
101+
columns:
102+
id:
103+
type: integer
104+
primary: true
105+
106+
...
107+
```
108+
86109
## Database tables
87110

88111
Traditionally, migrations are used to create database tables and modify them. In Leaf MVC, every schema file is tied to a particular table which is the name of the file. All you need to do is modify the columns of the table using the `columns` key in your schema file. Here's an example:
@@ -162,17 +185,17 @@ In the end, this means you can continue to use `php leaf db:rollback` to roll ba
162185

163186
Database seeds are a way to populate a database with initial data. This initial data can be used to set up default values or pre-populate a database with test data. Database seeds typically contain small amounts of data, such as default settings, test data, or sample records.
164187

165-
Seeders are used to populate your database with dummy data. In Leaf MVC, you can create seeders in your database files. The `seeders` key in your database file is used to create seeders. Here's an example of a seeder:
188+
Seeders are used to populate your database with dummy data. In Leaf MVC, you can create seeders in your database files. The `seeds` key in your database file is used to create seeders. Here's an example of a seeder:
166189

167190
```yml [users.yml]
168191
seeds:
169192
data:
170193
- name: 'Example User'
171194
172-
password: 'hash:password'
195+
password: '@hash:passwordForThisUser' # @hash requires leafs/password
173196
- name: 'Another User'
174197
175-
password: 'hash:password'
198+
password: '@hash:passwordForThisUser' # @hash requires leafs/password
176199
```
177200

178201
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.
@@ -185,7 +208,7 @@ seeds:
185208
data:
186209
name: 'Example User'
187210
188-
password: 'hash:password'
211+
password: '@hash:password'
189212
```
190213

191214
After creating your seeder, you can run your seeders using the `db:seed` command:
@@ -196,15 +219,15 @@ php leaf db:seed
196219

197220
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.
198221

199-
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:
222+
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:
200223

201224
```yml{4,5} [users.yml]
202225
seeds:
203226
count: 10
204227
data:
205-
name: 'faker:name'
206-
email: 'faker:email'
207-
password: 'hash:password'
228+
name: '@faker.name'
229+
email: '@faker.email'
230+
password: '@hash:password'
208231
```
209232

210233
In this example, we're generating 10 fake seeds for the `users` table.

0 commit comments

Comments
 (0)