作者 赵彬吉

update

... ... @@ -167,20 +167,19 @@ class Logic
if(!$ids){
$this->fail('ID不能为空');
}
$map[] = ['id', 'in', $ids];
$res = $this->formatQuery($map)->delete();
if($res){
if($this->is_cache){
foreach ($ids as $id){
$model = $this->getCacheInfo($id);
if(!$model){
continue;
}
$model->delete();
if($this->is_cache){
Cache::forget($this->getInfoCacheKey($id));
}
}
return $this->success();
}else{
$this->fail('删除失败');
}
}
/**
... ...
... ... @@ -3,6 +3,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
class Base extends Model
... ... @@ -15,6 +16,41 @@ class Base extends Model
'created_at' => 'datetime:Y-m-d H:i:s',
'updated_at' => 'datetime:Y-m-d H:i:s',
];
/**
* 监听模型事件
* @author zbj
* @date 2023/4/25
*/
protected static function booted()
{
//模型实例操作才会触发
//保存前数据 $row->original['xx']
//保存后数据 $row->xx
static::saved(function ($row) {
//删除缓存
$row->original && static::clearCache($row);
});
static::deleted(function ($row) {
//删除缓存
$row->original && static::clearCache($row);
});
}
/**
* 删除缓存 子类重写此方法
*
* @param $row
* @return bool
* @author zbj
* @date 2023/4/25
*/
public static function clearCache($row){
return true;
}
/**
* 日期序列化 勿删 删了时间就不是东八区时间了哈
* @param \DateTimeInterface $date
... ...
... ... @@ -56,4 +56,9 @@ class Menu extends Base
}
return $data;
}
public static function clearCache($row){
$cache_key = 'manage_menu_' . $row->original['route_name'];
Cache::forget($cache_key);
}
}
... ...