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
This will look in the "users" table for the row with the `id` of 1 and return it.
182
182
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
+
183
206
## Updating data in a database
184
207
185
208
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