How7o
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Reading: How to Get Config Variables in Laravel
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > 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.
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
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
Laravel get config variable — config() helper and Config facade resolving dotted keys
How to Get Config Variables in Laravel
May 3, 2026
Laravel call controller from another controller — app() and constructor injection patterns
How to Call a Controller Method from Another Controller in Laravel
May 3, 2026
Laravel Carbon not found error — namespace import fix
Fix “Class Carbon not found” in Laravel
May 3, 2026
Laravel unknown column CONCAT fix — DB::raw and selectRaw bypass identifier escaping
How to Fix “Unknown column ‘CONCAT'” in Laravel
May 3, 2026
Laravel validate in rule — restricting input to an allow-list with in: and Rule::in
Laravel Validate Input to Specific Values (in Rule)
May 3, 2026

You Might Also Like

Laravel last inserted ID — Eloquent save populates model primary key illustration
Web Development

How to Retrieve the Last Inserted ID in Laravel Eloquent

8 Min Read
Create custom exception class in Laravel (Artisan command + secure error handling)
Web Development

How to Create a Custom Exception Class in Laravel (With Clean JSON Responses)

6 Min Read
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 a variable in Laravel Blade using the @php directive
Web Development

How to Set a Variable in Laravel Blade Templates (With Examples)

6 Min Read
How7o

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

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?