/** * @param array $fields * * @return Budget */ public static function firstOrCreateEncrypted(array $fields) { // everything but the name: $query = Budget::orderBy('id'); $search = $fields; unset($search['name']); foreach ($search as $name => $value) { $query->where($name, $value); } $set = $query->get(['budgets.*']); /** @var Budget $budget */ foreach ($set as $budget) { if ($budget->name == $fields['name']) { return $budget; } } // create it! $budget = Budget::create($fields); return $budget; }