Skip to content

Commit d2da7ad

Browse files
committed
feat: add View::query()
1 parent bb073ae commit d2da7ad

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ class RecentOrdersView extends View
220220
}
221221
```
222222

223+
Additionally, you can start a query against the view using the `query()` method:
224+
225+
```php
226+
<?php
227+
228+
RecentOrdersView::query()
229+
->where('created_at', '>=', now()->subDays(30))
230+
->get();
231+
```
232+
223233
<!-- #### 💿 Materialized View -->
224234
<!---->
225235
#### 📐 Function

src/View.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use CalebDW\SqlEntities\Concerns\DefaultSqlEntityBehaviour;
88
use CalebDW\SqlEntities\Contracts\SqlEntity;
9+
use Illuminate\Database\Query\Builder;
10+
use Illuminate\Support\Facades\DB;
911

1012
abstract class View implements SqlEntity
1113
{
@@ -53,4 +55,12 @@ public function isRecursive(): bool
5355
{
5456
return $this->recursive;
5557
}
58+
59+
public static function query(?string $as = null): Builder
60+
{
61+
$instance = app(static::class);
62+
63+
return DB::connection($instance->connectionName())
64+
->table($instance->name(), $as);
65+
}
5666
}

0 commit comments

Comments
 (0)