Skip to content

Commit 26e69e8

Browse files
committed
update readme
1 parent f807caf commit 26e69e8

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77
## Why is this package needed?
88

9-
Now, if you want the sum or find the maximum column value in the related model, you will have two database queries.
9+
With standard use of Laravel, if you want the sum or find the maximum column value in the related model, you will have two database queries.
1010
With this methods, it all turns into one query to the database and there is no need to load extra data.
1111
It is also possible to sort by related models. And this sorting works with all types of relations.
12+
Added ability to load relations with a limit for each model without multiple queries.
1213
I often use this in my work and I hope it will be useful to you!
1314

1415
## Say thank you
@@ -108,3 +109,18 @@ By default, sorting is by `max` and `desc`, you can choose one of the options `m
108109
```php
109110
$invoices = Invoice::orderByRelation('items:price', 'asc', 'sum')->get();
110111
```
112+
113+
### Limit relations
114+
115+
If you want to load related model with limit, simply use the following method:
116+
```php
117+
$invoices = Invoice::all();
118+
$invoices->loadLimit('items:1');
119+
```
120+
or with conditions
121+
```php
122+
$invoices->loadLimit(['items:2', 'goods:1' => function ($query) {
123+
$query->orderBy('id', 'desc')->where('price', '<', 6);
124+
}]);
125+
```
126+
Note that first you write the name of the relation, and then the number of rows.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "alexmg86/laravel-sub-query",
33
"description": "Laravel subquery",
44
"license": "MIT",
5-
"keywords": ["php", "laravel", "sub", "query", "withSum"],
5+
"keywords": ["php", "laravel", "sub", "query", "withSum", "withMax", "withMin", "withAvg", "orderByRelation", "limit relation"],
66
"type": "library",
77
"authors": [
88
{

0 commit comments

Comments
 (0)