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/index.md
+50-17Lines changed: 50 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,24 +52,28 @@ Databases are essential for most applications, as they help you store and retrie
52
52
53
53
## Leaf MVC + DB
54
54
55
-
<divclass="grid md:grid-cols-2 gap-4">
55
+
Leaf's DB module is great for building simple queries, especially when you are using Leaf as a micro-framework. However, if you are building a full-fledged application using Leaf MVC, you can take advantage of the powerful models and schema files which make it easy to interact with your database.
56
+
57
+
Leaf MVC configures everything for you out of the box, so you just need to define your database schema using the schema files and create models to represent your database tables. You can then use the models to perform CRUD operations on your database without writing any SQL queries.
You can use Leaf DB to build and run queries that don't fit into a model. Everything has been configured to work out of the box, so you can start querying your database right away.
You can use Leaf DB to build and run queries that don't fit into a model, without any config.
96
129
</p>
97
130
<Button
98
131
as="a"
@@ -117,7 +150,7 @@ Databases are essential for most applications, as they help you store and retrie
117
150
118
151
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:
119
152
120
-
```php
153
+
```php:no-line-numbers
121
154
db()->connect([
122
155
'dbtype' => '...',
123
156
'charset' => '...',
@@ -135,7 +168,7 @@ Here are some examples of how you can connect to different databases:
135
168
136
169
::: code-group
137
170
138
-
```php [MySQL]
171
+
```php:no-line-numbers [MySQL]
139
172
db()->connect([
140
173
'host' => '127.0.0.1',
141
174
'username' => 'root',
@@ -144,7 +177,7 @@ db()->connect([
144
177
]);
145
178
```
146
179
147
-
```php [PostgreSQL]
180
+
```php:no-line-numbers [PostgreSQL]
148
181
db()->connect([
149
182
'dbtype' => 'pgsql',
150
183
'host' => '127.0.0.1',
@@ -155,7 +188,7 @@ db()->connect([
155
188
]);
156
189
```
157
190
158
-
```php [SQLite]
191
+
```php:no-line-numbers [SQLite]
159
192
db()->connect([
160
193
'dbtype' => 'sqlite',
161
194
'dbname' => 'db.sqlite',
@@ -170,7 +203,7 @@ Leaf DB will not connect to your database until you run a query. This means that
170
203
171
204
Some applications may need to connect to multiple databases for things like queues and logs, and Leaf DB allows you to keep multiple connections open and query them independently. Here's how you can connect to multiple databases:
172
205
173
-
```php
206
+
```php:no-line-numbers
174
207
db()->addConnections([
175
208
'conn1' => [
176
209
'dbtype' => '...',
@@ -185,7 +218,7 @@ db()->addConnections([
185
218
186
219
The `addConnections()` method takes an array of connection details for your databases as its first argument and the default connection name as its second argument. You can then switch between connections using the `useConnection()` method:
187
220
188
-
```php
221
+
```php:no-line-numbers
189
222
db('conn2')->select('users')->all();
190
223
```
191
224
@@ -203,7 +236,7 @@ $users = db()->query('SELECT * FROM users')->all();
203
236
204
237
The `query()` method takes an SQL query that you want to execute as its argument. You can then use the query builder methods to modify your query. For example, you can bind values to your query using the `bind()` method:
205
238
206
-
```php
239
+
```php:no-line-numbers
207
240
db()
208
241
->query('SELECT * FROM users WHERE id = ?')
209
242
->bind('1')
@@ -232,7 +265,7 @@ This will return an array of all the users in the database that match the query.
232
265
233
266
If you only want to get one result, you can use the `fetchObj()` or `fetchAssoc()` method. For example, you can run a query like this:
0 commit comments