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/builder.md
+14-8Lines changed: 14 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ Although you can write raw queries using the `query()` method, there's no fun in
6
6
7
7
This is something you would usually want to do outside of your application, but there are a few rare cases where you might want to create a database from within your application. Leaf DB provides a `create()` method that allows you to do just that.
8
8
9
-
```php
9
+
```php:no-line-numbers
10
10
db()->create('dbname')->execute();
11
11
```
12
12
@@ -26,7 +26,7 @@ db()
26
26
27
27
Dropping a database is the opposite of creating one. It deletes the database and all its contents. Leaf DB provides a `drop()` method that allows you to do this.
28
28
29
-
```php
29
+
```php:no-line-numbers
30
30
db()->drop('dbname')->execute();
31
31
```
32
32
@@ -40,13 +40,13 @@ This method needs the name of the table you want to add data to, like "users", a
Note that this may not work correctly if your database uses non-auto-incrementing IDs like UUIDs or ULIDs.
93
+
92
94
## Reading data from a database
93
95
94
96
Reading from a database means retrieving data stored in a table. Leaf DB provides a `select()` method that allows you to build a query to retrieve data from a table. The `select()` method takes the name of the table you want to read from as its argument.
95
97
96
-
```php
98
+
```php:no-line-numbers
97
99
db()->select('users')->all();
98
100
```
99
101
100
102
This will return all the rows in the users table. You can also specify the columns you want to return by passing them as the second argument to the `select()` method.
101
103
102
-
```php
104
+
```php:no-line-numbers
103
105
db()->select('users', 'name, created_at')->all();
104
106
```
105
107
@@ -172,7 +174,7 @@ db()
172
174
173
175
Almost every database table has an `id` column that uniquely identifies each row. Leaf DB provides a `find()` method that allows you to retrieve a row by its `id`.
174
176
175
-
```php
177
+
```php:no-line-numbers
176
178
db()->select('users')->find(1);
177
179
```
178
180
@@ -209,7 +211,7 @@ Deleting data from a database works by finding the data you want to delete and t
209
211
210
212
Here's an example:
211
213
212
-
```php
214
+
```php:no-line-numbers
213
215
db()->delete('users')->execute(); // careful now 🙂
214
216
```
215
217
@@ -259,6 +261,10 @@ if ($success) {
259
261
260
262
This is useful especially when you have a set of queries that rely on third party influence.
261
263
264
+
::: warning Rollback not working
265
+
Transactions will only work correctly if your queries use Leaf DB. This is because your queries need to use the same database connection to be able to be rolled back. This means you can't use transactions with your Leaf MVC models at the moment, but this may change in the future.
266
+
:::
267
+
262
268
## Hiding columns from results
263
269
264
270
Sometimes you might want to hide certain columns from the results of a query. For instance, you might want to hide the password column from the results of a query on the users table. Leaf DB provides a `hide()` method that allows you to do this.
0 commit comments