Skip to content

Commit 42c088f

Browse files
committed
feat: add relationships docs
1 parent 84ddd25 commit 42c088f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/docs/database/builder.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,29 @@ db()->select('users')->find(1);
180180

181181
This will look in the "users" table for the row with the `id` of 1 and return it.
182182

183+
## Data relationships
184+
185+
Sometimes you might want to retrieve data from multiple tables. For example, you might want to retrieve all the posts by a user. Leaf DB provides a `with()` method that allows you to do this.
186+
187+
```php
188+
db()
189+
->select('users')
190+
->with('posts')
191+
->all();
192+
```
193+
194+
This will return all the users in the users table along with their posts. It also works the same way for getting a single row.
195+
196+
```php
197+
db()
198+
->select('users')
199+
->where('id', 1)
200+
->with('posts')
201+
->first();
202+
```
203+
204+
The `with()` method works the same way whether you are dealing with a `hasMany` or `belongsTo` relationship.
205+
183206
## Updating data in a database
184207

185208
Updating data in a database works by finding the data you want to update and then passing new data in to change the existing data. Leaf DB provides an `update()` method that allows you to build a query to update data in a table. You need to pair this with the `params()` method to specify the new data you want to update.

0 commit comments

Comments
 (0)