60 lines
1.4 KiB
PHP
60 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\Tasks\Infrastructure\DBQueries;
|
|
|
|
use Modules\Src\TraitSqlQuery;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Database\Query\Builder;
|
|
|
|
class TasksQuery
|
|
{
|
|
|
|
use TraitSqlQuery;
|
|
|
|
private string $table;
|
|
|
|
private Builder $queryBuilder;
|
|
|
|
public function setTable(string $table): void
|
|
{
|
|
$this->table = $table;
|
|
}
|
|
|
|
public function select(array $fields = []): self
|
|
{
|
|
$allowFields = ['id', 'title', 'created_at',];
|
|
$select = $this->getSelectSection($fields, $allowFields, $this->table);
|
|
$this->queryBuilder = DB::table($this->table);
|
|
$this->queryBuilder->select($select);
|
|
return $this;
|
|
}
|
|
|
|
public function whereId(string $id): self
|
|
{
|
|
$this->queryBuilder->where($this->table . '.id', '=', $id);
|
|
return $this;
|
|
}
|
|
|
|
public function limit(int $limit, int $offset): self
|
|
{
|
|
$this->queryBuilder->limit($limit)->offset($offset);
|
|
return $this;
|
|
}
|
|
|
|
public function all(): array
|
|
{
|
|
$result = [];
|
|
foreach ($this->queryBuilder->get()->all() as $row) {
|
|
$result[$row->id] = $row;
|
|
}
|
|
return json_decode(json_encode($result), true);
|
|
}
|
|
|
|
public function one(): array
|
|
{
|
|
$result = $this->queryBuilder->first() ?? [];
|
|
$result = json_decode(json_encode($result), true);
|
|
return $result;
|
|
}
|
|
}
|