Skip to content

Commit 5bbd4f1

Browse files
committed
added methods whereCurrentYear, whereCurrentMonth, whereCurrentDay
1 parent a4c6364 commit 5bbd4f1

File tree

6 files changed

+79
-3
lines changed

6 files changed

+79
-3
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-sub-query` will be documented in this file
44

5+
## [v1.9.9 (2021-04-14)](https://github.com/Alexmg86/laravel-sub-query/compare/v1.9.8...v1.9.9)
6+
### Added
7+
- methods whereCurrentYear, whereCurrentMonth, whereCurrentDay
8+
59
## [v1.9.8 (2021-04-14)](https://github.com/Alexmg86/laravel-sub-query/compare/v1.9.7...v1.9.8)
610
### Fixed
711
- fixed some methods for laravel version 8.*

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ $items = Item::withMath(['invoice_id', 'price', 'price2'], '*', 'new_column')->g
133133
echo $items[0]->new_column;
134134
```
135135

136+
### Working with dates
137+
138+
A more convenient way to select by dates. By default, sorting is by `created_at`.
139+
```php
140+
$invoices = Invoice::whereCurrentYear('column_name')->get();
141+
$invoices = Invoice::whereCurrentMonth()->get();
142+
$invoices = Invoice::whereCurrentDay()->get();
143+
```
144+
136145
### Load latest or oldest relation
137146

138147
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

+30
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,34 @@ public function forceIndex($column)
101101
{
102102
return $this->query->from(\DB::raw($this->query->from . ' FORCE INDEX (' . $column . ')'));
103103
}
104+
105+
/**
106+
* Find items for current year
107+
* @param string $column
108+
* @return $this
109+
*/
110+
public function whereCurrentYear($column = 'created_at')
111+
{
112+
return $this->whereRaw("$column between date_format(now() ,'%Y-01-01') and now()");
113+
}
114+
115+
/**
116+
* Find items for current year
117+
* @param string $column
118+
* @return $this
119+
*/
120+
public function whereCurrentMonth($column = 'created_at')
121+
{
122+
return $this->whereRaw("$column between date_format(now() ,'%Y-%m-01') and now()");
123+
}
124+
125+
/**
126+
* Find items for current day
127+
* @param string $column
128+
* @return $this
129+
*/
130+
public function whereCurrentDay($column = 'created_at')
131+
{
132+
return $this->whereRaw("$column between date_format(now() ,'%Y-%m-%d') and now()");
133+
}
104134
}

tests/DatabaseTestCase.php

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected function createInvoicesTable(): void
3737
Schema::create('invoices', function (Blueprint $table) {
3838
$table->increments('id');
3939
$table->string('name', 100);
40+
$table->timestamps();
4041
});
4142
}
4243

tests/LaravelSubQuerySugarTest.php

+32-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,38 @@ public function testForceIndex()
9191
{
9292
$this->createBasic();
9393

94-
$result = Invoice::forceIndex('name')->toSql();
94+
$results = Invoice::forceIndex('name')->toSql();
9595

96-
$this->assertEquals('select * from invoices FORCE INDEX (name)', $result);
96+
$this->assertEquals('select * from invoices FORCE INDEX (name)', $results);
97+
}
98+
99+
public function testCurrentYear()
100+
{
101+
$this->createBasic();
102+
103+
$results = Invoice::whereCurrentYear()->toSql();
104+
105+
$need = "select * from \"invoices\" where created_at between date_format(now() ,'%Y-01-01') and now()";
106+
$this->assertEquals($need, $results);
107+
}
108+
109+
public function testCurrentMonth()
110+
{
111+
$this->createBasic();
112+
113+
$results = Invoice::whereCurrentMonth()->toSql();
114+
115+
$need = "select * from \"invoices\" where created_at between date_format(now() ,'%Y-%m-01') and now()";
116+
$this->assertEquals($need, $results);
117+
}
118+
119+
public function testCurrentDay()
120+
{
121+
$this->createBasic();
122+
123+
$results = Invoice::whereCurrentDay()->toSql();
124+
125+
$need = "select * from \"invoices\" where created_at between date_format(now() ,'%Y-%m-%d') and now()";
126+
$this->assertEquals($need, $results);
97127
}
98128
}

tests/Models/Invoice.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ class Invoice extends Model
1010
use LaravelSubQueryTrait;
1111

1212
public $table = 'invoices';
13-
public $timestamps = false;
13+
1414
protected $guarded = ['id'];
1515

16+
protected $hidden = ['created_at', 'updated_at'];
17+
1618
public function items()
1719
{
1820
return $this->hasMany(Item::class, 'invoice_id');

0 commit comments

Comments
 (0)