Skip to content

Commit 58d0426

Browse files
committed
some sugar
1 parent 40d2234 commit 58d0426

File tree

5 files changed

+135
-4
lines changed

5 files changed

+135
-4
lines changed

README.md

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

1515
## Last added
1616

17-
Added caching of [received data](https://github.com/Alexmg86/laravel-sub-query#caching)
17+
2020/10/21 - Some sugar [received data](https://github.com/Alexmg86/laravel-sub-query#sugar)
18+
2020/10/06 - Added caching of [received data](https://github.com/Alexmg86/laravel-sub-query#caching)
1819

1920
## Say thank you
2021

@@ -158,3 +159,8 @@ Invoice::withSum('items:price')->remember(now()->addDay())->posts()->get();
158159
Invoice::withSum('items:price')->remember(60 * 60 * 24)->get();
159160
```
160161
A more detailed description [is here](https://github.com/Alexmg86/laravel-sub-query/wiki/Cache)
162+
163+
### Sugar
164+
165+
I got tired of writing some things in detail and I decided to remove them in methods.
166+
You can see [it here](https://github.com/Alexmg86/laravel-sub-query/wiki/Some-sugar)

src/LaravelSubQuery.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Alexmg86\LaravelSubQuery;
44

5+
use Alexmg86\LaravelSubQuery\Traits\LaravelSubQuerySugar;
56
use Closure;
67
use Illuminate\Database\Eloquent\Builder;
78
use Illuminate\Database\Eloquent\Concerns\QueriesRelationships;
@@ -13,6 +14,7 @@
1314

1415
class LaravelSubQuery extends Builder
1516
{
17+
use LaravelSubQuerySugar;
1618
use QueriesRelationships;
1719

1820
/**
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Alexmg86\LaravelSubQuery\Traits;
4+
5+
use Illuminate\Database\Query\Expression;
6+
7+
trait LaravelSubQuerySugar
8+
{
9+
/**
10+
* like query left side
11+
*/
12+
public function likeLeft($column, $value)
13+
{
14+
return $this->like($column, '%' . $value, true);
15+
}
16+
17+
/**
18+
* like query right side
19+
*/
20+
public function likeRight($column, $value)
21+
{
22+
return $this->like($column, $value . '%', true);
23+
}
24+
25+
/**
26+
* like query both side
27+
*/
28+
public function like($column, $value, $condition = false)
29+
{
30+
$value = $condition ? $value : '%' . $value . '%';
31+
return $this->query->where($column, 'like', $value);
32+
}
33+
34+
/**
35+
* casting column in query
36+
* date, datetime, time, char, signed, unsigned, binary
37+
*/
38+
public function castColumn($column, $type = null)
39+
{
40+
$columns = [];
41+
if (!is_array($column)) {
42+
$columns[$column] = $type;
43+
} else {
44+
$columns = $column;
45+
}
46+
47+
foreach ($columns as $key => $type) {
48+
$this->addSelect(new Expression("CAST($key as $type) as $key"));
49+
}
50+
return $this;
51+
}
52+
}

tests/DatabaseTestCase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ protected function createGoodsTable(): void
6363
protected function createCountriesTable(): void
6464
{
6565
Schema::create('countries', function (Blueprint $table) {
66-
$table->increments('id');
66+
$table->id();
6767
$table->string('name');
6868
});
6969
}
7070

7171
protected function createCustomersTable(): void
7272
{
7373
Schema::create('customers', function (Blueprint $table) {
74-
$table->increments('id');
74+
$table->id();
7575
$table->integer('country_id');
7676
$table->string('name');
7777
});
@@ -80,7 +80,7 @@ protected function createCustomersTable(): void
8080
protected function createPostsTable(): void
8181
{
8282
Schema::create('posts', function (Blueprint $table) {
83-
$table->increments('id');
83+
$table->id();
8484
$table->integer('user_id');
8585
$table->string('title');
8686
});

tests/LaravelSubQuerySugarTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Alexmg86\LaravelSubQuery\Tests;
4+
5+
use Alexmg86\LaravelSubQuery\Facades\LaravelSubQuery;
6+
use Alexmg86\LaravelSubQuery\ServiceProvider;
7+
use Alexmg86\LaravelSubQuery\Tests\Models\Invoice;
8+
9+
class LaravelSubQuerySugarTest extends DatabaseTestCase
10+
{
11+
protected function getPackageProviders($app)
12+
{
13+
return [ServiceProvider::class];
14+
}
15+
16+
protected function getPackageAliases($app)
17+
{
18+
return [
19+
'laravel-sub-query' => LaravelSubQuery::class,
20+
];
21+
}
22+
23+
private function createBasic()
24+
{
25+
foreach (['first', 'second', 'third'] as $name) {
26+
Invoice::create(['name' => $name]);
27+
}
28+
}
29+
30+
public function testLikeLeft()
31+
{
32+
$this->createBasic();
33+
34+
$results = Invoice::likeLeft('name', 'st')->count();
35+
36+
$this->assertEquals(1, $results);
37+
}
38+
39+
public function testLikeRight()
40+
{
41+
$this->createBasic();
42+
43+
$results = Invoice::likeRight('name', 'sec')->count();
44+
45+
$this->assertEquals(1, $results);
46+
}
47+
48+
public function testLike()
49+
{
50+
$this->createBasic();
51+
52+
$results = Invoice::like('name', 'ir')->count();
53+
54+
$this->assertEquals(2, $results);
55+
}
56+
57+
public function testCastColumn()
58+
{
59+
foreach (['111', '10', '2', '22'] as $name) {
60+
Invoice::create(['name' => $name]);
61+
}
62+
63+
$results = Invoice::orderByDesc('name')->first();
64+
65+
$this->assertEquals(22, $results->name);
66+
67+
$results = Invoice::castColumn('name', 'signed')->orderByDesc('name')->first();
68+
69+
$this->assertEquals(111, $results->name);
70+
}
71+
}

0 commit comments

Comments
 (0)