public function generateCode() { if ($this->code == null) { $code = substr(md5(microtime()), rand(0, 26), 8); while (Snippet::where('code', $code)->count() != 0) { $code = substr(md5(microtime()), rand(0, 26), 8); } $this->code = $code; return $code; } }