where('used', self::USED) ->find(); } /** * 默认使用 * * @time 2021年03月08日 * @param int $id * @return mixed */ public function used(int $id) { $t = $this->findBy($id); $t->used = self::USED; if ($t->save()) { self::where('id', '<>', $id) ->where('model_id', $t->model_id) ->update([ 'used' => self::NOT_USE, ]); return $t; } throw new FailedException('启用失败'); } /** * 获取使用 * * @time 2021年03月08日 * @param $modelId * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @return array|\think\Model|null */ public function getUsed($modelId) { return $this->where('model_id', $modelId) ->where('used', self::USED) ->find(); } }