-
All my models have the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Ok for the moment i found a solution for me. With a custom Replaced Trait: <?php
namespace App\Models\Traits;
use Abbasudo\Purity\Traits\Filterable;
use App\Filters\SoftDeletes\Resolve;
use Illuminate\Database\Eloquent\SoftDeletes;
trait FilterableSoftDeletes {
use Filterable;
public function initializeFilterableSoftDeletes(): void
{
$traits = get_declared_traits();
if(!in_array(SoftDeletes::class,$traits)){
return;
}
$this->defaultFilterResolverClass = Resolve::class;
}
} The Filter Resolver <?php
namespace App\Filters\SoftDeletes;
use Abbasudo\Purity\Filters\FilterList;
use Abbasudo\Purity\Filters\Resolve as AbbasudoResolve;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class Resolve extends AbbasudoResolve
{
private Model $model;
public function __construct(FilterList $filterList, Model $model)
{
parent::__construct($filterList, $model);
$this->model = $model;
}
public function apply(Builder $query, string $field, array|string $values): void
{
if ($field === $this->model->getDeletedAtColumn()) {
$query->withTrashed();
}
parent::apply($query, $field, $values);
}
} $aircraft = new App\Models\Aircraft();
$builder = $aircraft->query()->filter(['deleted_at' => ['$gte' => '2021-01-01T00:00:00Z']]);
echo $builder->toRawSql().PHP_EOL;
// select * from `aircraft` where `deleted_at` >= '2021-01-01T00:00:00Z'
$builder = $aircraft->query()->filter(['updated_at' => ['$gte' => '2021-01-01T00:00:00Z']]);
echo $builder->toRawSql().PHP_EOL;
// select * from `aircraft` where `updated_at` >= '2021-01-01T00:00:00Z' and `aircraft`.`deleted_at` is null |
Beta Was this translation helpful? Give feedback.
-
Hey @tantchen. class TrashedFilter extends Filter
{
/**
* Operator to detect in the query params.
*
* @var string
*/
protected static string $operator = '$wt';
/**
* Apply filter logic to $query.
*
* @return Closure
*/
public function apply(): Closure
{
return function ($query) {
if ($this->value) {
$query->withTrashed();
}
};
}
} This way end users have more control over results. |
Beta Was this translation helpful? Give feedback.
-
Feel free to make a PR adding a new trashed filter with tests. or any solution to this problem. |
Beta Was this translation helpful? Give feedback.
Hey @tantchen.
Nice solution. however , i suggest creating a new filter using
make:filter
command as described in docsThis way end users have more control over results.