Skip to content

Commit 73944a9

Browse files
committed
added method withMath
1 parent 541d9f2 commit 73944a9

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ I often use this in my work and I hope it will be useful to you!
1414

1515
## Last added
1616

17+
2020/11/03 - Added method [withMath](https://github.com/Alexmg86/laravel-sub-query#working-with-columns)
1718
2020/10/21 - Added [some sugar](https://github.com/Alexmg86/laravel-sub-query#sugar)
1819
2020/10/06 - Added caching of [received data](https://github.com/Alexmg86/laravel-sub-query#caching)
1920

@@ -115,6 +116,19 @@ By default, sorting is by `max` and `desc`, you can choose one of the options `m
115116
$invoices = Invoice::orderByRelation('items:price', 'asc', 'sum')->get();
116117
```
117118

119+
### Working with columns
120+
121+
To add or multiply the required columns use this method:
122+
```php
123+
$items = Item::withMath(['invoice_id', 'price'])->get();
124+
echo $items[0]->sum_invoice_id_price;
125+
```
126+
Columns will be summed by default, you can choose one of the options `+`, `-`, `*`, `/` and set a new name.
127+
```php
128+
$items = Item::withMath(['invoice_id', 'price', 'price2'], '*', 'new_column')->get();
129+
echo $items[0]->new_column;
130+
```
131+
118132
### Load latest or oldest relation
119133

120134
Imagine you want to get a list of 50 accounts, each with 100 items. By default, you will get 5000 positions and select the first ones for each account. PHP smokes nervously on the sidelines.

src/Traits/LaravelSubQuerySugar.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,46 @@ public function castColumn($column, $type = null)
4949
}
5050
return $this;
5151
}
52+
53+
/**
54+
* Column math
55+
* @param string[] $columns
56+
* @param string $operator
57+
* @param string|null $name
58+
* @return $this
59+
*/
60+
public function withMath($columns, $operator = '+', $name = null)
61+
{
62+
$default = [
63+
'+' => 'sum_',
64+
'-' => 'sub_',
65+
'*' => 'multi_',
66+
'/' => 'div_'
67+
];
68+
69+
if (!is_array($columns) || count($columns) < 2) {
70+
return $this;
71+
}
72+
73+
if (is_null($this->query->columns)) {
74+
$this->query->select([$this->query->from . '.*']);
75+
}
76+
77+
$query = [];
78+
foreach ($columns as $column) {
79+
$query[] = $this->query->from . "." . $column;
80+
}
81+
$query = implode(" $operator ", $query);
82+
83+
$asName = implode('_', $columns);
84+
if ($name) {
85+
$asName = $name;
86+
} elseif (array_key_exists($operator, $default)) {
87+
$asName = $default[$operator] . $asName;
88+
} else {
89+
$asName = 'custom_' . $asName;
90+
}
91+
92+
return $this->addSelect(new Expression("$query as $asName"));
93+
}
5294
}

tests/LaravelSubQuerySugarTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Alexmg86\LaravelSubQuery\Facades\LaravelSubQuery;
66
use Alexmg86\LaravelSubQuery\ServiceProvider;
77
use Alexmg86\LaravelSubQuery\Tests\Models\Invoice;
8+
use Alexmg86\LaravelSubQuery\Tests\Models\Item;
89

910
class LaravelSubQuerySugarTest extends DatabaseTestCase
1011
{
@@ -68,4 +69,21 @@ public function testCastColumn()
6869

6970
$this->assertEquals(111, $results->name);
7071
}
72+
73+
public function testWithMaths()
74+
{
75+
Item::create(['invoice_id' => 1, 'price' => 2, 'price2' => 5]);
76+
77+
$results = Item::withMath(['price', 'price2'])->first();
78+
79+
$this->assertEquals(7, $results->sum_price_price2);
80+
81+
$results = Item::withMath(['price', 'price2'], '*')->first();
82+
83+
$this->assertEquals(10, $results->multi_price_price2);
84+
85+
$results = Item::withMath(['price', 'price2'], '*', 'multi')->first();
86+
87+
$this->assertEquals(10, $results->multi);
88+
}
7189
}

tests/Models/Item.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Alexmg86\LaravelSubQuery\Tests\Models;
44

5+
use Alexmg86\LaravelSubQuery\Traits\LaravelSubQueryTrait;
56
use Illuminate\Database\Eloquent\Model;
67

78
class Item extends Model
89
{
10+
use LaravelSubQueryTrait;
11+
912
public $table = 'items';
1013
public $timestamps = false;
1114
protected $guarded = ['id'];

0 commit comments

Comments
 (0)