public function store($companyId, $request) { $cacheTag = ['companies', 'company' . $companyId, 'domains']; $cacheKey = 'index'; $_companies = Companies::find($companyId); if ($_companies) { if (!empty($request['domains'])) { foreach ($request['domains'] as $value) { if (strpos($value, 'localhost') !== 0) { $validator = $this->validate($value); if ($validator->fails()) { return new \Exception($validator->errors()->first()); } } if (Domains::where('domain', '=', $value)->where('company_id', '<>', $companyId)->count() > 0) { return new \Exception('O domínio ' . $value . ' já está sendo usado por outro company'); } } $_companies->domains()->delete(); foreach ($request['domains'] as $value) { Logs::create(['activity' => 'store', 'module' => 'companies_domains', 'ref' => $value]); $_companies->domains()->create(['company_id' => $companyId, 'domain' => $value]); } Cache::tags($cacheTag)->flush(); return true; } else { return true; } } else { return new \Exception("Houve um erro ao localizar registro (id not found)"); } }
private static function getIdByOrigin($origin) { $cacheTag = ['origins']; $cacheKey = $origin; $data = Cache::tags($cacheTag)->rememberForever($cacheKey, function () use($origin) { $_company = self::where('uri', $origin); if ($_company->count() > 0) { return $_company->first()->id; } else { $_domain = \App\Models\Companies\Domains::select('company_id')->where('domain', $origin); if ($_domain->count() > 0) { return $_domain->first()->company_id; } else { return false; } } }); if (!$data) { Cache::tags($cacheTag)->forget($cacheKey); } return $data; }