hasMany(Participation::class, 'code_unique_id'); } // * FUNCTIONS /** * Generate code_unique attribute * * @return string|null */ public function getCodeUniqueAttribute() { if (!$this->id or !\is_integer($this->id) or $this->id < 1) { return null; } $codes = Cache::remember('codes', 86400, function () { $codes = Storage::disk('export_codes_uniques')->get('Codes.csv'); $codes = preg_replace('#\s+#', ',', trim($codes)); $codes = explode(',', $codes); return collect($codes)->flip(); }); $codes = $codes->flip(); return $codes->get($this->id - 1); // return self::intToSerial($this->id); } /** * Generate a serial from an int. * * @param integer $int * @return string The serial * @throws SerialCasterException If an error happens du to config. */ public static function intToSerial(int $int): string { return SerialCaster::encode( $int, \abs(\crc32(config('app.key'))), app(Settings::class)->code_length, config('codes.codeCharList') ); } /** * Generate a serial from an int. * * @param string $serial * @return integer The ID represented * @throws SerialCasterException If an error happens due to config or wrong serial is given. */ public static function serialToInt(string $serial): int { return SerialCaster::decode( $serial, \abs(\crc32(config('app.key'))), config('codes.codeCharList') ); } }