Skip to content

[12.x] Add config()->first() helper for fallback keys#59130

Open
tabuna wants to merge 1 commit intolaravel:12.xfrom
tabuna:12.x
Open

[12.x] Add config()->first() helper for fallback keys#59130
tabuna wants to merge 1 commit intolaravel:12.xfrom
tabuna:12.x

Conversation

@tabuna
Copy link
Contributor

@tabuna tabuna commented Mar 8, 2026

This PR adds a tiny, expressive first() method to the config repository (and thus the config() helper) that returns the first existing value from a list of keys, falling back to a default if none exist.

Motivation

Configuration keys sometimes move between files or get renamed in major releases / packages.
To maintain backward compatibility, packages and applications frequently do:

$value = config('new.key') ?? config('old.key') ?? 'default';

// or worse:
$value = collect(config()->get(['new.key', 'old.key'], 'default'))->first();

The second variant is especially noisy in Blade:

@extends(collect(config()->get(['layout', 'theme.layout'], 'app'))->first())

Proposed solution

Add first() method:

$value = config()->first(['new.key', 'old.key'], 'default');
@extends(config()->first(['layout', 'theme.layout'], 'app'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant