How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Get Config Variables in Laravel
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Get Config Variables in Laravel
Web Development

How to Get Config Variables in Laravel

how7o
By how7o
Last updated: May 3, 2026
7 Min Read
Laravel get config variable — config() helper and Config facade resolving dotted keys
SHARE

To laravel get config variable in a controller, model, service, or Blade view, use the config() helper or the Config facade with a dotted key path. The first segment names the file in config/, and the rest drills into the array it returns. This guide covers both forms, why you reach for them instead of env(), and the production-only footgun that config:cache introduces.

Contents
  • TL;DR
  • How Laravel config files work
  • Both helper forms work the same way
  • Reading keys from other config files
  • config() vs env() — always prefer config()
  • Runtime overrides
  • After changing a config file
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-23 on Laravel 11 with PHP 8.3. Originally published 2022-11-28, rewritten and updated 2026-04-23.

TL;DR

Use config('app.name') anywhere in Laravel code. In Blade: <title>Accounts - {{ config('app.name') }}</title>. For a nested key inside config/database.php: config('database.default'). Never call env('APP_NAME') outside a config/*.php file — it returns null in production once the config is cached.

How Laravel config files work

Every file under config/ returns a PHP array. config/app.php might start like:

// config/app.php
return [
    'name' => env('APP_NAME', 'BMS'),
    'env'  => env('APP_ENV', 'production'),
    // ...
];

Laravel loads every file in config/ at boot time and merges them into a single repository keyed by file name. config('app.name') is literally “open config/app.php‘s returned array, then read ['name'] from it.” The dot-notation syntax works for nested arrays too — config('app.providers.0') returns the first provider string.

Both helper forms work the same way

// In a Blade view
<title>Accounts - {{ Config::get('app.name') }}</title>

// Same thing, shorter
<title>Accounts - {{ config('app.name') }}</title>

The Config facade and the config() helper resolve to the same config repository and return identical values. The helper is more idiomatic in Blade views and controllers; the facade is fine when you prefer a “class-like” style or when your IDE autocompletion prefers classes over helpers.

Reading keys from other config files

Swap the first segment for the file name. For config/database.php:

config('database.default');                 // e.g. "mysql"
config('database.connections.mysql.host');  // e.g. "127.0.0.1"

For your own config file config/features.php:

// config/features.php
return [
    'billing_v2' => env('FEATURE_BILLING_V2', false),
];

// Anywhere in the app
if (config('features.billing_v2')) {
    // new billing path
}
laravel get config variable — .env feeds config files feeds config() helper

config() vs env() — always prefer config()

env('APP_NAME') only works when the config cache isn’t built. In production the first thing a sensible deploy script does is php artisan config:cache, which pre-resolves every config/*.php file into a single cached PHP array at bootstrap/cache/config.php. Once that cache exists, Laravel skips .env entirely — env() returns null everywhere except inside the config files themselves.

The rule is simple: env() belongs only in config/*.php. Controllers, models, services, middleware, Blade views, and commands always call config(). If you find an env() call outside config, refactor it: add the value to a config file, reference it via config('file.key'), and the production config:cache step will stop silently breaking it.

Runtime overrides

Passing an array to config() sets a value for the current request only:

config(['app.name' => 'Accounts']);

// Later in the request
config('app.name');  // "Accounts"

The override never touches the config file or the cache; it lives in memory for this request and resets on the next one. Useful for tests and one-off overrides — risky as a production pattern because the coupling between the override site and the read site is invisible to any future reader.

After changing a config file

If the environment is running with a built config cache, you need to rebuild it after editing any config/*.php file or touching .env:

php artisan config:clear   # remove the cache
php artisan config:cache   # rebuild it

Locally you can skip this — without a built cache, Laravel reads the files on every request and changes show up immediately. In CI/CD, always config:cache after deploying new code.

Frequently asked questions

What’s the simplest laravel get config variable call?

config('app.name'). The first segment is the config file name (config/app.php → app), the rest is the dotted key path into its returned array. The equivalent facade form is Config::get('app.name'); both hit the same cached repository and return the same value.

What’s the difference between config() and env()?

env() reads directly from .env and only works when the config cache is not built. config() reads from the resolved config/*.php arrays, which do go through env() at build time. In any controller, service, or Blade view, always call config() — calling env() there silently returns null in production once php artisan config:cache has run.

How do I set a default if the key is missing?

Pass a second argument: config('app.name', 'BMS'). If app.name isn’t defined in the config file (or resolves to null), Laravel returns the default instead. Useful for optional custom keys you add to config/app.php that older environments might not have.

Can I change a config value at runtime?

Yes: config(['app.name' => 'Accounts']). The change is in-memory only — it lasts for the current request and never touches the config file. Avoid writing to config in middleware or services that run on every request; it couples unrelated code to the value’s key name. Prefer service classes that own their own state.

Why does config() work in production but env() doesn’t?

After php artisan config:cache, Laravel bypasses .env entirely and reads from bootstrap/cache/config.php. The config cache is a pre-resolved PHP array, built once at deploy time. env() outside a config file returns null in that mode by design — it forces you to route all configuration through the cached config layer, which is fast and deterministic.

Related guides

  • How to Set a Global Variable for Laravel Views — pushing config values into every Blade template with a view composer.
  • How to Install Laravel on Ubuntu — where .env and config/ show up in a fresh install.
  • How to Run a Laravel Project Without a .env File — config resolution when the env file is missing.
  • How to Call a Controller Method from Another Controller in Laravel — another pattern that uses the service container.

References

Official Laravel configuration docs (config helper, Config facade, config caching): laravel.com/docs/configuration.

TAGGED:configurationLaravelphp

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Laravel call controller from another controller — app() and constructor injection patterns How to Call a Controller Method from Another Controller in Laravel
Next Article Laravel global variable for views — View::share in AppServiceProvider and View::composer wildcard patterns How to Set a Global Variable for Laravel Views
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Laravel Eloquent ORM — a model class mapping to a database table with query methods
Laravel Eloquent ORM: The Complete Guide to Querying Your Database
June 16, 2026
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026

You Might Also Like

Return or throw an error in Laravel (JSON response vs exception)
Web Development

How to Manually Return or Throw an Error Exception in Laravel

6 Min Read
Set A4 paper size in CSS for printing
Web Development

How to Set A4 Paper Size in CSS for Print

5 Min Read
Open a file selection dialog from a button click
Web Development

How to Open a File Dialog When Clicking a Button

4 Min Read
Validate an email address in PHP with filter_var
Web Development

How to Validate an Email Address in PHP

5 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?