findByKey($key); if (is_null($existing->id)) { $importJob = new ImportJob; $importJob->user()->associate($this->user); $importJob->file_type = $fileType; $importJob->key = Str::random(12); $importJob->status = 'import_status_never_started'; $importJob->extended_status = [ 'total_steps' => 0, 'steps_done' => 0, 'import_count' => 0, 'importTag' => 0, 'errors' => [], ]; $importJob->save(); // breaks the loop: return $importJob; } $count++; } return new ImportJob; } /** * @param string $key * * @return ImportJob */ public function findByKey(string $key): ImportJob { $result = $this->user->importJobs()->where('key', $key)->first(['import_jobs.*']); if (is_null($result)) { return new ImportJob; } return $result; } /** * @param User $user */ public function setUser(User $user) { $this->user = $user; } }