Skip to content

Commit 545d5a2

Browse files
committed
added alias
1 parent be72b85 commit 545d5a2

6 files changed

+31
-10
lines changed

README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ echo $invoices[0]->items_price_avg;
4343
```
4444
### The following methods apply to all methods!!!
4545

46-
You may add the "sum" for multiple relations as well as add constraints to the queries:
46+
You may add the sum for multiple relations as well as add constraints to the queries:
4747
```php
4848
use Illuminate\Database\Eloquent\Builder;
4949

@@ -55,6 +55,17 @@ echo $invoices[0]->items_price_sum;
5555
echo $invoices[0]->goods_price_sum;
5656
echo $invoices[0]->goods_price2_sum;
5757
```
58+
You may also alias the relationship sum result, allowing multiple sums on the same relationship:
59+
```php
60+
use Illuminate\Database\Eloquent\Builder;
61+
62+
$invoices = Invoice::withSum(['items:price', 'goods:price as sum_goods_price' => function (Builder $query) {
63+
$query->where('price','!=',1);
64+
}])->get();
65+
66+
echo $invoices[0]->items_price_sum;
67+
echo $invoices[0]->sum_goods_price;
68+
```
5869
If you're combining `withSum` with a `select` statement, ensure that you call `withSum` after the `select` method:
5970
```php
6071
$invoices = Invoice::select(['id'])->withSum('items:price')->get();

src/LaravelSubQuery.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ protected function withSubQuery($relations, $type)
7171
$relations = is_array($relations) ? $relations : array_slice(func_get_args(), 0, 1);
7272

7373
foreach ($this->parseForSubQueryRelations($relations) as $name => $constraints) {
74+
$segments = explode(' ', $name);
75+
76+
unset($alias);
77+
78+
if (count($segments) === 3 && Str::lower($segments[1]) === 'as') {
79+
[$name, $alias] = [$segments[0], $segments[2]];
80+
}
81+
7482
$nameExplode = explode(':', $name);
7583
$name = $nameExplode[0];
7684
$columns = isset($nameExplode[1]) ? explode(',', $nameExplode[1]) : [];
@@ -103,7 +111,9 @@ protected function withSubQuery($relations, $type)
103111
// Finally we will add the proper result column alias to the query and run the subselect
104112
// statement against the query builder. Then we will return the builder instance back
105113
// to the developer for further constraint chaining that needs to take place on it.
106-
$this->selectSub($query, Str::snake($name.'_'.$column.'_'.$type));
114+
$column = $alias ?? Str::snake($name.'_'.$column.'_'.$type);
115+
116+
$this->selectSub($query, $column);
107117
}
108118
}
109119

tests/LaravelSubQueryWithAvgTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ public function testWithSelect()
8585
Item::create(['invoice_id' => $invoice->id, 'price' => $i, 'price2' => $i + 1]);
8686
}
8787

88-
$results = Invoice::select(['id'])->withAvg('items:price');
88+
$results = Invoice::select(['id'])->withAvg('items:price as price_avg');
8989

9090
$this->assertEquals([
91-
['id' => 1, 'items_price_avg' => 5.5],
91+
['id' => 1, 'price_avg' => 5.5],
9292
], $results->get()->toArray());
9393
}
9494

tests/LaravelSubQueryWithMaxTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ public function testWithSelect()
8585
Item::create(['invoice_id' => $invoice->id, 'price' => $i, 'price2' => $i + 1]);
8686
}
8787

88-
$results = Invoice::select(['id'])->withMax('items:price');
88+
$results = Invoice::select(['id'])->withMax('items:price as price_max');
8989

9090
$this->assertEquals([
91-
['id' => 1, 'items_price_max' => 10],
91+
['id' => 1, 'price_max' => 10],
9292
], $results->get()->toArray());
9393
}
9494

tests/LaravelSubQueryWithMinTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ public function testWithSelect()
8585
Item::create(['invoice_id' => $invoice->id, 'price' => $i, 'price2' => $i + 1]);
8686
}
8787

88-
$results = Invoice::select(['id'])->withMin('items:price');
88+
$results = Invoice::select(['id'])->withMin('items:price as price_min');
8989

9090
$this->assertEquals([
91-
['id' => 1, 'items_price_min' => 1],
91+
['id' => 1, 'price_min' => 1],
9292
], $results->get()->toArray());
9393
}
9494

tests/LaravelSubQueryWithSumTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ public function testWithSelect()
8787
Item::create(['invoice_id' => $invoice->id, 'price' => $i, 'price2' => $i + 1]);
8888
}
8989

90-
$results = Invoice::select(['id'])->withSum('items:price');
90+
$results = Invoice::select(['id'])->withSum('items:price as price_sum');
9191

9292
$this->assertEquals([
93-
['id' => 1, 'items_price_sum' => 55],
93+
['id' => 1, 'price_sum' => 55],
9494
], $results->get()->toArray());
9595
}
9696

0 commit comments

Comments
 (0)