[QUESTION] 软删除为什么会失效
Closed this issue · 1 comments
hhw2444 commented
调用这个方法的时候
$list = $this->repository->getQuery($param)->select(['sn', 'name', 'id'])->get();
软删除会失效
model:
<?php
declare(strict_types=1);
namespace App\Model\Saas;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property string $sn 商城编号
* @property string $name 商城名称
* @property string $contact 联系人
* @property string $contact_mobile 联系人手机号
* @property int $status 状态 0-停止服务 1-开启服务
* @property string $domain_alias 域名别名
* @property int $set_meal_id 套餐id
* @property int $expires_time 到期时间
* @property string $remark 备注
* @property int $create_time
* @property int $update_time
* @property int $delete_time
*/
class PlatformShop extends SaasBaseModel
{
use SoftDeletes;
public const CREATED_AT = 'create_time';
public const UPDATED_AT = 'update_time';
public const DELETED_AT = 'delete_time';
public bool $timestamps = false;
/**
* The table associated with the model.
*/
protected ?string $table = 'platform_shop';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = ['id', 'sn', 'name', 'contact', 'contact_mobile', 'status', 'domain_alias', 'set_meal_id', 'expires_time', 'remark', 'create_time', 'update_time', 'delete_time'];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'status' => 'integer', 'set_meal_id' => 'integer', 'expires_time' => 'integer', 'create_time' => 'integer', 'update_time' => 'integer', 'delete_time' => 'integer'];
}
repository:
<?php
namespace App\Repository\Saas;
use App\Model\Saas\PlatformShop;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;
use Hyperf\Collection\Collection;
use Hyperf\Database\Model\Builder;
use Hyperf\DbConnection\Db;
class PlatformShopRepository extends IRepository
{
public function __construct(
protected readonly PlatformShop $model,
){}
public function handleSearch(Builder $query, array $params): Builder
{
return $query
->when(Arr::get($params, 'sortable'), static function (Builder $query, array $value) {
$query->orderBy(key($value), current($value));
})
->when(Arr::get($params, 'ids'), static function (Builder $query, $value) {
$query->whereIn('id', $value);
})
->when(Arr::get($params, 'name'), static function (Builder $query, $value) {
$query->where('name', 'like', "%{$value}%");
})
->when(Arr::get($params, 'status'), static function (Builder $query, $value) {
$query->where('status', '=', $value);
});
}
}
zds-s commented
更新到最新版本。重写 Repository->getQuery
方法
类似这样
class YourRepository extends IRepository {
public function getQuery(): Builder{
return $this->model::withTrashed();
}
}