order('id', 'desc') ->catchSearch() ->catchOrder() ->paginate(); } /** * * * @time 2020年06月01日 * @param $data ['driver' => '', 'path' => '', 'url' => ], * @param UploadedFile $file * @return Attachments|Model */ public static function store($data, UploadedFile $file) { return parent::create([ 'file_size' => $file->getSize(), 'mime_type' => $file->getMime(), 'file_ext' => $file->getOriginalExtension(), 'filename' => $file->getOriginalName(), 'driver' => $data['driver'], 'url' => $data['url'], 'path' => $data['path'] ]); } /** * 批量删除 * * @time 2021年03月01日 * @param $id * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @return bool */ public function deletes($id): bool { Utils::setFilesystemConfig(); $this->whereIn('id', Utils::stringToArrayBy($id)) ->select() ->each(function ($attachment){ if ($attachment->delete()) { if ($attachment->driver == 'local') { $localPath = config('filesystem.disks.local.root') . DIRECTORY_SEPARATOR; $path = str_replace('\\','\/', $attachment->path); if (file_exists($localPath . $path)) { Filesystem::delete($path); } } else { Filesystem::delete($attachment->path); } } }); return true; } }