Skip to content

Commit

Permalink
ref #226
Browse files Browse the repository at this point in the history
  • Loading branch information
aocneanu committed Nov 26, 2020
1 parent 126d4e1 commit fed12de
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
11 changes: 8 additions & 3 deletions src/Services/Data/Builders/Meta.php
Expand Up @@ -62,9 +62,14 @@ public function toArray(): array

public function count($filtered = false): int
{
return $filtered || ! $this->table instanceof CustomCount
? $this->query->getQuery()->getCountForPagination()
: $this->table->count();
if ($this->table instanceof CustomCount && ! $filtered) {
return $this->table->count();
}

return $this->query
->when($this->config->get('softDeletes'), fn ($query) => $query
->whereNull('deleted_at'))
->getQuery()->getCountForPagination();
}

public function filter(): self
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Data/Config.php
Expand Up @@ -10,7 +10,7 @@ class Config
{
private const TemplateProxy = [
'appends', 'comparisonOperator', 'countCache', 'flatten', 'fullInfoRecordLimit',
'name', 'strip',
'name', 'strip', 'softDeletes',
];

private const RequestMeta = [
Expand Down
28 changes: 21 additions & 7 deletions src/Services/Template.php
Expand Up @@ -2,6 +2,8 @@

namespace LaravelEnso\Tables\Services;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
Expand Down Expand Up @@ -93,26 +95,38 @@ private function builder()
private function template()
{
$template = $this->readJson($this->table->templatePath());
$model = $this->table->query()->getModel();

if (! $template->has('model')) {
$this->setModel($template);
$this->setModel($template, $model);
}
$this->setTable($template);

$this->setTable($template, $model)
->setSoftDeletes($template, $model);

return $template;
}

private function setModel(Obj $template)
private function setModel(Obj $template, Model $model)
{
$model = (new ReflectionClass($this->table->query()->getModel()))
->getShortName();
$model = (new ReflectionClass($model))->getShortName();

$template->set('model', Str::camel($model));
}

private function setTable(Obj $template)
private function setTable(Obj $template, Model $model): self
{
$template->set('table', $model->getTable());

return $this;
}

private function setSoftDeletes(Obj $template, Model $model)
{
$template->set('table', $this->table->query()->getModel()->getTable());
$traits = (new ReflectionClass($model))->getTraitNames();
$template->set('softDeletes', in_array(SoftDeletes::class, $traits));

return $this;
}

private function readJson($path)
Expand Down

0 comments on commit fed12de

Please sign in to comment.