$value]); if (strlen($value) === 0) { return new Budget; } /** @var BudgetRepositoryInterface $repository */ $repository = app(BudgetRepositoryInterface::class, [$this->user]); if (isset($this->mapping[$value])) { Log::debug('Found budget in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]); $budget = $repository->find(intval($this->mapping[$value])); if (!is_null($budget->id)) { Log::debug('Found budget by ID', ['id' => $budget->id]); return $budget; } } // not mapped? Still try to find it first: $budget = $repository->findByName($value); if (!is_null($budget->id)) { Log::debug('Found budget by name ', ['id' => $budget->id]); return $budget; } // create new budget. Use a lot of made up values. $budget = $repository->store( [ 'name' => $value, 'user_id' => $this->user->id, ] ); return $budget; } }