logService = $logService; $this->logService->menuTitle = 'translation'; } /** * @param array $data * @throws Exception */ public function createModel(array $data) { try { DB::beginTransaction(); DB::commit(); $save_data = [ 'en' => $data['en'], 'zh_cn' => $data['zh_cn'], 'zh_tw' => $data['zh_tw'] ]; if (AdminTranslation::query()->where($save_data)->exists()) { throw new Exception( __('service.admin_translation.data_exists') ); } $save_data['created_at'] = get_datetime(); $model = AdminTranslation::query()->create($save_data); $this->logService->logCreated($model, 'translation.create'); return $model; } catch (Exception $e) { DB::rollBack(); throw $e; } } /** * @param array $data * @param int $id * @throws Exception */ public function updateModel(array $data, int $id) { try { DB::beginTransaction(); DB::commit(); $update_data = [ 'en' => $data['en'], 'zh_cn' => $data['zh_cn'], 'zh_tw' => $data['zh_tw'] ]; if (AdminTranslation::query()->where($update_data)->where( 'id', '<>', $id )->exists() ) { throw new Exception( __('service.admin_translation.data_exists') ); } $model = AdminTranslation::query()->findOrFail($id); $oldValues = $model->toArray(); $update_data['updated_at'] = get_datetime(); $model->update($update_data); $this->logService->logUpdated($model, $oldValues, 'translation.update'); return $model; } catch (Exception $e) { DB::rollBack(); throw $e; } } /** * @param int $id * @return bool * @throws Exception */ public function deleteModel(int $id): bool { try { DB::beginTransaction(); $model = AdminTranslation::query()->findOrFail($id); $this->logService->logDeleted($model, 'translation.delete'); $model->delete(); DB::commit(); return true; } catch (Exception $e) { DB::rollBack(); throw $e; } } }