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/factories.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ Factories are a way to conveniently generate large amounts of database records.
10
10
11
11
To create a factory, you can use the `g:factory` command. This command will create a new factory class in the `app/database/factories` directory.
12
12
13
-
```bash
13
+
```bash:no-line-numbers
14
14
php leaf g:factory UserFactory
15
15
```
16
16
@@ -58,7 +58,7 @@ This factory class defines the attributes for the `User` model. The `definition`
58
58
59
59
To use a factory to generate records, you can use the `create` method on the factory class. This method will create a new record in the database using the attributes defined in the factory.
60
60
61
-
```php
61
+
```php:no-line-numbers
62
62
(new UserFactory)->create(20)->save();
63
63
```
64
64
@@ -92,14 +92,14 @@ This will generate 20 records in the `users` table using the attributes defined
92
92
93
93
In some cases, you may not want to save the generated records to the database. You can use the `get()` method to get the generated data without saving it to the database.
94
94
95
-
```php
95
+
```php:no-line-numbers
96
96
$users = (new UserFactory)->create(20)->get();
97
97
```
98
98
99
99
## Running Factories
100
100
101
101
You don't need to run factories manually. You can use the `db:seed` command to run your seeders, which will in turn run your factories.
Copy file name to clipboardExpand all lines: src/docs/database/index.md
+39-35Lines changed: 39 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,6 +49,41 @@ Databases are essential for most applications, as they help you store and retrie
49
49
50
50
:::
51
51
52
+
## Leaf MVC + DB
53
+
54
+
Leaf MVC comes with built-in support for models which are a way to programmatically represent resources in your database using PHP classes. For that reason, you have no real need for this module unless you want to use Leaf Auth. If you choose to use Leaf DB in your MVC application, we have already set up everything for you. All you need to do is to head over to your `.env` file and set up your database connection details. Here are a few example connections:
55
+
56
+
::: code-group
57
+
58
+
```txt [MySQL]
59
+
DB_CONNECTION=mysql
60
+
DB_HOST=127.0.0.1
61
+
DB_PORT=3306
62
+
DB_DATABASE=LeafMVC
63
+
DB_USERNAME=root
64
+
DB_PASSWORD=
65
+
```
66
+
67
+
```txt [PostgreSQL]
68
+
DB_CONNECTION=pgsql
69
+
DB_HOST=127.0.0.1
70
+
DB_PORT=5432
71
+
DB_DATABASE=LeafMVC
72
+
DB_USERNAME=root
73
+
DB_PASSWORD=
74
+
```
75
+
76
+
```txt [SQLite]
77
+
DB_CONNECTION=sqlite
78
+
DB_DATABASE=/absolute/path/to/database.sqlite
79
+
```
80
+
81
+
:::
82
+
83
+
Remember to head over to `public/index.php` and uncomment the line that says `\Leaf\Database::initDb();`. This will automatically connect to your database using the details in your environment file.
84
+
85
+
You can safely skip the "Connecting to a database" section.
86
+
52
87
## Connecting to a database
53
88
54
89
The first step to using a database is to create a connection. It's like opening a door to the database, allowing you to interact with it. Here's how you can connect to a database using Leaf:
@@ -100,44 +135,13 @@ db()->connect([
100
135
101
136
:::
102
137
103
-
If you are using Leaf MVC, we have already set up everything for you. All you need to do is to head over to your `.env` file and set up your database connection details.
104
-
105
-
::: code-group
106
-
107
-
```txt [MySQL]
108
-
DB_CONNECTION=mysql
109
-
DB_HOST=127.0.0.1
110
-
DB_PORT=3306
111
-
DB_DATABASE=LeafMVC
112
-
DB_USERNAME=root
113
-
DB_PASSWORD=
114
-
```
115
-
116
-
```txt [PostgreSQL]
117
-
DB_CONNECTION=pgsql
118
-
DB_HOST=127.0.0.1
119
-
DB_PORT=5432
120
-
DB_DATABASE=LeafMVC
121
-
DB_USERNAME=root
122
-
DB_PASSWORD=
123
-
```
124
-
125
-
```txt [SQLite]
126
-
DB_CONNECTION=sqlite
127
-
DB_DATABASE=/absolute/path/to/database.sqlite
128
-
```
129
-
130
-
:::
131
-
132
-
Remember to head over to `public/index.php` and uncomment the line that says `\Leaf\Database::initDb();`. This will automatically connect to your database using the details in your environment file.
133
-
134
138
## Writing simple queries
135
139
136
140
Once you've connected to a database, you can start writing queries to interact with it. Queries are the commands you run on your database to get, insert, update or delete data. Leaf DB provides a simple way to run queries using the query builder, but also allows you to run raw SQL queries.
137
141
138
142
We can run queries using the `query()` method. This method takes in a query string and returns a query builder instance. This means that you can run queries like this:
139
143
140
-
```php
144
+
```php:no-line-numbers
141
145
$users = db()->query('SELECT * FROM users')->all();
142
146
```
143
147
@@ -158,13 +162,13 @@ There are different kinds of database commands: some give you results (like data
158
162
159
163
You can use `execute()` to run queries that don't return values. This method returns `true` if the query was successful and `false` if it wasn't. You can run a query like this:
160
164
161
-
```php
165
+
```php:no-line-numbers
162
166
db()->query('CREATE DATABASE dbname')->execute();
163
167
```
164
168
165
169
If you want to run a query that returns data, you can use the `all()` method to get all the results. For example, you can run a query like this:
166
170
167
-
```php
171
+
```php:no-line-numbers
168
172
$users = db()->query('SELECT * FROM users')->all();
169
173
```
170
174
@@ -183,6 +187,6 @@ This will return the matched user as an object.
183
187
184
188
There may be times when you want to get a single value from a query that returns multiple rows. In such cases, you can use the `first()` method. For example, you can run a query like this:
185
189
186
-
```php
190
+
```php:no-line-numbers
187
191
$user = db()->query('SELECT * FROM users')->first();
Copy file name to clipboardExpand all lines: src/docs/database/models.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,7 @@ This code will retrieve all rows from the `flights` table and then loop through
51
51
52
52
The Leaf all method will return all of the results in the model's table. Since each Leaf model serves as a query builder, you may also add constraints to queries, and then use the get method to retrieve the results:
Copy file name to clipboardExpand all lines: src/docs/http/cors.md
+18Lines changed: 18 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -79,6 +79,24 @@ app()->cors([
79
79
80
80
This will allow `http://example.com`, `https://example.com`, `http://www.example.com`, and `https://some-subdomain.example.com` to access your app. Of course, you can also use a regular expression to match multiple domains. You can find a full list of options below.
81
81
82
+
## CORS + Leaf MVC
83
+
84
+
If you are using Leaf MVC, you can configure CORS using your environment variables in place of the configuration above:
While this is easier and allows you to easily configure different environments, it can sometimes be limiting for example when you want to return a function for dynamically set your allowed origins. For this reason, you can publish your CORS configuration using the command below:
93
+
94
+
```bash:no-line-numbers
95
+
php leaf config:publish cors
96
+
```
97
+
98
+
This will create or update your CORS config in `config/cors.php`. You can then use the options below to configure the CORS module to suit your exact needs.
99
+
82
100
## Configuration Options
83
101
84
102
The `cors()` method takes in an array of options. Here are the available options:
You can check if a header exists using the `Leaf\Http\Headers::has()` method. This method takes in a single parameter, the header to check and returns a boolean.
Copy file name to clipboardExpand all lines: src/docs/mvc/commands.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -82,7 +82,7 @@ class CachePurgeCommand extends Command
82
82
}
83
83
```
84
84
85
-
If you are using Leaf MVC v3.8 and above, the command is automatically loaded by Leaf MVC. If you are using Leaf MVC v3.7 and below, you will need to register the command in the `app/console/Commands.php` file.n
85
+
If you are using Leaf MVC v3.8 and above, the command is automatically loaded by Leaf MVC. If you are using Leaf MVC v3.7 and below, you will need to register the command in the `app/console/Commands.php` file.
86
86
87
87
```php
88
88
<?php
@@ -112,7 +112,7 @@ class Commands
112
112
113
113
Command arguments are values that are passed to the command when it is run in the console. For example, if you have a command named `example` and you run it like this:
114
114
115
-
```bash
115
+
```bash:no-line-numbers
116
116
php leaf example argument
117
117
```
118
118
@@ -138,7 +138,7 @@ protected function handle()
138
138
139
139
Command options are values that are passed to the command when it is run in the console. For example, if you have a command named `example` and you run it like this:
140
140
141
-
```bash
141
+
```bash:no-line-numbers
142
142
php leaf example --option=value
143
143
```
144
144
@@ -371,6 +371,6 @@ This method asks a question but hides the keystrokes. It takes in 2 parameters:
371
371
- the question to ask
372
372
- use hidden fallback (optional)
373
373
374
-
```php
374
+
```php:no-line-numbers
375
375
$password = $this->secret('Confirm your password');
Copy file name to clipboardExpand all lines: src/docs/mvc/console.md
+14Lines changed: 14 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,20 @@ Leaf MVC includes a powerful command-line tool called Aloe to help you manage yo
8
8
php leaf list
9
9
```
10
10
11
+
::: details Missing commands?
12
+
13
+
If you get errors from commands which you saw in the documentation, you are probably running an older version of the Leaf MVC console. We add more handy commands regularly, but as the console does not automatically update, you may run into the missing command error. To fix that problem, you need to install the latest version of Aloe:
14
+
15
+
```bash:no-line-numbers
16
+
leaf install aloe
17
+
18
+
# or with composer
19
+
20
+
composer require leafs/aloe
21
+
```
22
+
23
+
:::
24
+
11
25
## Aloe vs. Leaf CLI: What's the Difference?
12
26
13
27
Before diving in, it’s important to know that Aloe is different from Leaf CLI.
0 commit comments