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
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. On top of that, Leaf MVC also introduced a new concept called schema files which allowed you to generate migrations from JSON data. While this was a great feature, it was a bit too much for a lot of developers and added to the growing hell of files in your app. To solve this, we've decided to move away from the Rails/Laravel way of doing things and introduce a new way of handling database files in Leaf MVC.
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.
4
4
5
-
## What are Database Files?
5
+
## What are Schema Files?
6
6
7
-
Database files are a way to handle migrations, seeders, and factories in a single file. This way, you can easily manage your database structure without having to create multiple files for each operation and without having to repeat yourself all over your app. Database files are written in yaml which makes them incredibly easy to read and write.
7
+
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.
8
8
9
9
```yml [flights.yml]
10
-
defaultId: true
10
+
increments: true
11
11
timestamps: true
12
12
columns:
13
13
to: string
@@ -17,23 +17,29 @@ columns:
17
17
seeds:
18
18
count: 10
19
19
data:
20
-
to: faker.city
21
-
from: faker.city
22
-
identifier: faker.uuid
20
+
to: 'faker:city'
21
+
from: 'faker:city'
22
+
identifier: 'faker:uuid'
23
23
```
24
24
25
-
## Creating a Database File
25
+
## Creating a Schema File
26
26
27
-
Aloe comes with a `g:db` command that you can use to generate a database file. You can generate a database file by running:
27
+
Aloe comes with a `g:schema` command that you can use to generate a database file. You can generate a database file by running:
28
28
29
29
```bash:no-line-numbers
30
-
php leaf g:db users
30
+
php leaf g:schema <table-name>
31
31
```
32
32
33
-
This will create a database file at `app/database/users.yml` which looks like this:
33
+
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:
34
+
35
+
```bash:no-line-numbers
36
+
php leaf g:schema users
37
+
```
38
+
39
+
This will create a schema file at `app/database/users.yml` which looks like this:
34
40
35
41
```yml [users.yml]
36
-
defaultId: true
42
+
increments: true
37
43
timestamps: true
38
44
columns:
39
45
name: string
@@ -47,14 +53,14 @@ columns:
47
53
seeds:
48
54
count: 10
49
55
data:
50
-
name: faker.name
51
-
email: faker.email
52
-
password: "{{ bcrypt('password') }}"
56
+
name: 'faker:name'
57
+
email: 'faker:email'
58
+
password: 'hash:password'
53
59
```
54
60
55
61
Breaking down this file, we have:
56
62
57
-
- `defaultId`: 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.
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.
58
64
59
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.
60
66
@@ -74,12 +80,12 @@ Breaking down this file, we have:
74
80
75
81
- `seeds`: This is used to set the seeders of the table. The available properties are:
76
82
- `count`: This is used to set the number of seeds to generate.
77
-
- `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.
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. -->
78
84
- `truncate`: This is used to truncate the table before seeding.
79
85
80
-
## DB Migrations
86
+
## Database tables
81
87
82
-
Traditionally, migrations are used to create database tables and modify them. In Leaf MVC, you can create migrations in your database files. The `columns` key in your database file is used to create migrations. Here's an example of a migration:
88
+
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:
83
89
84
90
```yml [users.yml]
85
91
columns:
@@ -92,27 +98,69 @@ columns:
92
98
email_verified_at: timestamp
93
99
```
94
100
95
-
In this example, we create a migration that creates a `users` table with `name`, `email`, `password`, and `email_verified_at` columns. To run your migrations, you can use the `db:migrate` command:
101
+
In this example, we create a `users` table with `name`, `email`, `password`, and `email_verified_at` columns. We can then migrate this table to our database using the `db:migrate` command:
96
102
97
103
```bash:no-line-numbers
98
104
php leaf db:migrate
99
105
```
100
106
101
-
<!-- ## DB File Scripts
107
+
You can have multiple schema files in your `app/database` directory, each tied to a particular table. When you run the `db:migrate` command, Leaf will migrate all the tables in your `app/database` directory. If you want to migrate only a specific table, you can pass the table name as an argument to the `db:migrate` command:
102
108
103
-
We understand that you might have some complicated functionality that you would want to run when migrating your database, which is why we allow you to run PHP scripts in your database files. This way, you can run any PHP code you want when migrating your database.
104
-
105
-
```yml [users.yml]
109
+
```bash:no-line-numbers
110
+
php leaf db:migrate users
106
111
```
107
112
108
-
Now you need to create the PHP script that will run when migrating your database. You can create a PHP script at `app/database/scripts/users.php`:
113
+
<!-- ## Database migrations vs data migrations
114
+
115
+
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.
109
116
110
-
```php [users.php]
117
+
Leaf MVC provides database scripts which you can use to handle your data migrations. Separating database migrations from data migrations allows you to safely roll-back your data migrations without affecting your database structure. Here's an example of a database script:
118
+
119
+
```php [ImportUsersFromOldTable.php]
120
+
<?php
121
+
122
+
use App\Models\User;
123
+
124
+
class ImportUsersFromOldTable
125
+
{
126
+
public function up()
127
+
{
128
+
$oldUsers = getOldUsersAndMapToNewUsers();
129
+
130
+
foreach ($oldUsers as $oldUser) {
131
+
User::create([
132
+
'name' => $oldUser->name,
133
+
'email' => $oldUser->email,
134
+
'password' => $oldUser->password,
135
+
'is_from_old_table' => true
136
+
]);
137
+
}
138
+
}
139
+
140
+
public function down()
141
+
{
142
+
User::where('is_from_old_table', true)->delete();
143
+
}
144
+
}
111
145
```
112
146
113
-
In this example, we're running a PHP script that creates a new table in the database, but checks if particular columns exist before creating the table. -->
147
+
Now you just need to run the script using the `db:script` command:
148
+
149
+
```bash:no-line-numbers
150
+
php leaf db:script ImportUsersFromOldTable
151
+
```-->
152
+
153
+
## Migration histories
154
+
155
+
Migration histories are used to keep track of the migrations that have been run on your database. This is useful for keeping track of the state of your database so you can easily roll back to a previous state if needed. Unlike in other frameworks, Leaf MVC does require you to manually create migrations to keep track of your migration history. This is done automatically for you.
156
+
157
+
Every time you edit a schema file and run the `db:migrate` command, Leaf will automatically keep track of the migrations that have been run on your database, which means less time scrambling through migration files and more time building your app.
158
+
159
+
In the end, this means you can continue to use `php leaf db:rollback` to roll back your database to a previous state.
114
160
115
-
## DB Seeders
161
+
## Seeding your database
162
+
163
+
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.
116
164
117
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:
In this example, we create a seeder that seeds the `users` table with an example user. We are passing an array of seeds to the `data` key, each seed being a key value pair of column name and value.
178
+
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.
128
179
129
-
If you want to generate multiple seeds, you can pass an object to the `data` key instead of an array together with a `count` key:
180
+
Another way to generate multiple seeds is to use the `count` key. When using the `count` key, you can pass an integer value to generate multiple seeds with the same data. Here's an example:
After creating your seeder, you can run your seeders using the `db:seed` command:
@@ -145,15 +196,27 @@ php leaf db:seed
145
196
146
197
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.
147
198
148
-
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:
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:
149
200
150
201
```yml{4,5} [users.yml]
151
202
seeds:
152
203
count: 10
153
204
data:
154
-
name: faker.name
155
-
email: faker.email
156
-
password: "{{ bcrypt('password') }}"
205
+
name: 'faker:name'
206
+
email: 'faker:email'
207
+
password: 'hash:password'
157
208
```
158
209
159
210
In this example, we're generating 10 fake seeds for the `users` table.
211
+
212
+
After adding your seeds, you can run your seeders using the `db:seed` command:
213
+
214
+
```bash:no-line-numbers
215
+
php leaf db:seed
216
+
```
217
+
218
+
If you want to seed a specific table, you can pass the table name as an argument to the `db:seed` command:
Copy file name to clipboardExpand all lines: src/docs/auth/index.md
+23-7Lines changed: 23 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,9 +32,11 @@ The next step is to link your database and start signing users in.
32
32
33
33
To do any kind of authentication, you need to connect to some kind of database which will store your users' data. If you are already using Leaf DB or Leaf MVC, then your database connection will automatically be used by Leaf Auth, so you don't need to connect to your database again.
34
34
35
-
If you are not using Leaf DB or Leaf MVC, you can connect to your database manually:
35
+
If you are **NOT** using Leaf DB or Leaf MVC, you can connect to your database manually:
36
36
37
-
```php
37
+
::: code-group
38
+
39
+
```php [Auth connect]
38
40
auth()->connect([
39
41
'dbtype' => '...',
40
42
'charset' => '...',
@@ -46,16 +48,16 @@ auth()->connect([
46
48
]);
47
49
```
48
50
49
-
If you have an existing PDO connection, you can pass it to Leaf Auth:
50
-
51
-
```php
51
+
```php [Existing PDO instance]
52
52
$db = new PDO('mysql:dbname=test;host=127.0.0.1', 'root', '');
53
53
54
54
auth()->dbConnection($db);
55
55
56
56
// you can use leaf auth the same way you always have
57
57
```
58
58
59
+
:::
60
+
59
61
## Auth + Leaf MVC
60
62
61
63
If you are using Leaf MVC, you can set up Leaf Auth to work with your default database connection by heading over to the `public/index.php` file and uncommenting the line that connects to the database:
@@ -90,12 +92,26 @@ Leaf Auth doesn't give you any structure for your database, with that, you can s
90
92
91
93
1. By default, Leaf Auth assumes that your database primary key is `id`. If you have a database where you are using another field, say `admin_id` as the primary key, you will need to tell Leaf the name of your primary key. You can do this using the `id.key` config:
2. Leaf Auth assumes that you will save your users in a database table named `users`, this might however not be the case for your application. If you want to use a different table, you can configure Leaf Auth using `db.table`:
Copy file name to clipboardExpand all lines: src/docs/auth/login.md
+61-3Lines changed: 61 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,15 +71,25 @@ If you want to use a couple of fields from the user within your application, you
71
71
72
72
Leaf uses token based authentication by default which uses a JWT to authenticate your users. Sessions are a more common way to authenticate users in fullstack applications. To switch to session based authentication, you can update your auth config:
With the addition of session auth, `login()` will automatically start a session, but will behave in the same way, which means redirects and any other functionality you need will be left up to you to handle:
Some applications only allow users to sign in using OAuth which means there's no need for users to add emails or passwords. Leaf Auth provides the `fromOAuth()` function which allows you to create a session or token for a user without needing a password.
If the user is successfully saved in the database, a session or token is created for them and the rest of the process is the same as signing up a user normally. If Leaf Auth fails to save the user, the method returns `false`. You can then use the `errors()` method to get the error message.
142
+
143
+
```php
144
+
$success = auth()->fromOAuth([
145
+
'token' => $token,
146
+
'user' => [
147
+
'name' => $user['name'],
148
+
'email' => $user['email'],
149
+
'avatar' => $user['avatar_url']
150
+
]
151
+
]);
152
+
153
+
if (!$success) {
154
+
$error = auth()->errors();
155
+
}
156
+
157
+
// user is authenticated
158
+
$user = auth()->user();
159
+
```
160
+
161
+
Everything after this point is the same as signing up a user normally.
162
+
163
+
::: info OAuth Token
164
+
The `fromOAuth()` method expects an OAuth token to be passed in. This token is usually gotten from the OAuth provider you are using. You can later use this token to make requests to the OAuth provider on behalf of the user. Leaf Auth saves this token so you can retrieve it later using the `auth()->oauthToken()` method.
165
+
166
+
```php
167
+
$token = auth()->oauthToken();
168
+
```
169
+
170
+
:::
171
+
114
172
## Auth with no password
115
173
116
174
Leaf Auth usually expects a password field to authenticate users. This is necessary because most applications require a password to authenticate users. The field is usually named `password`, however, you can configure Leaf Auth to expect a different field:
0 commit comments