Bird
Raised Fist0
Laravelframework~20 mins

Config files and access in Laravel - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Config Mastery in Laravel
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
1:30remaining
What is the output of accessing a config value?
Given the Laravel config file config/app.php contains 'name' => 'MyApp', what will config('app.name') return?
Laravel
<?php
// config/app.php
return [
    'name' => 'MyApp',
];

// In a controller or route
$value = config('app.name');
echo $value;
Anull
B"app.name"
CThrows an error
D"MyApp"
Attempts:
2 left
💡 Hint
Think about how Laravel accesses nested config keys using dot notation.
📝 Syntax
intermediate
1:30remaining
Which option correctly sets a default value when accessing config?
You want to get the config value app.timezone, but provide a default of 'UTC' if it is not set. Which code does this correctly?
Aconfig('app.timezone' || 'UTC')
Bconfig('app.timezone', 'UTC')
Cconfig('app.timezone' ?: 'UTC')
Dconfig('app.timezone') ?? 'UTC'
Attempts:
2 left
💡 Hint
Check the Laravel config helper signature for default values.
🔧 Debug
advanced
2:00remaining
Why does this config access cause an error?
Consider this code snippet:
config('database.connections.mysql.host');
The database.php config file returns an array but the connections key is missing. What error will this cause?
Laravel
<?php
// database.php
return [
    // 'connections' key is missing
];

// Accessing
$host = config('database.connections.mysql.host');
Anull
BUndefined index error
CThrows an exception: InvalidArgumentException
DTypeError
Attempts:
2 left
💡 Hint
Think about how Laravel handles missing config keys when accessing nested keys.
🧠 Conceptual
advanced
2:00remaining
How to override config values at runtime?
Which method correctly changes a config value during runtime in Laravel?
AConfig::set('app.debug', true);
Bconfig('app.debug', true);
Cconfig(['app.debug' => true]);
Dconfig()->set('app.debug', true);
Attempts:
2 left
💡 Hint
Check the Laravel config helper usage for setting values.
❓ state_output
expert
1:30remaining
What is the output after changing config at runtime?
Given this code sequence:
config(['app.locale' => 'fr']);
echo config('app.locale');
What will be printed?
Laravel
config(['app.locale' => 'fr']);
echo config('app.locale');
A"fr"
B"en"
Cnull
DThrows an error
Attempts:
2 left
💡 Hint
Remember that config changes at runtime affect subsequent calls.

Practice

(1/5)
1. What is the primary purpose of config files in a Laravel application?
easy
A. To define routes for the application
B. To write database queries
C. To create user interface components
D. To store application settings as PHP arrays

Solution

  1. Step 1: Understand config file role

    Config files in Laravel are PHP files that return arrays holding settings.
  2. Step 2: Identify correct purpose

    They store app settings like database, mail, and cache configurations.
  3. Final Answer:

    To store application settings as PHP arrays -> Option D
  4. Quick Check:

    Config files = app settings [OK]
Hint: Config files hold settings, not code or UI [OK]
Common Mistakes:
  • Confusing config files with route or view files
  • Thinking config files contain SQL queries
  • Assuming config files define UI elements
2. Which of the following is the correct syntax to access the 'timezone' setting from the 'app' config file in Laravel?
easy
A. config('app.timezone')
B. config('app->timezone')
C. Config::get('timezone.app')
D. Config::get('app.timezone')

Solution

  1. Step 1: Recall Laravel config access syntax

    Laravel uses the helper function config('file.key') to get config values.
  2. Step 2: Identify correct syntax

    config('app.timezone') uses the correct helper function and dot notation for 'app.timezone'.
  3. Final Answer:

    config('app.timezone') -> Option A
  4. Quick Check:

    Use config('file.key') for access [OK]
Hint: Use config() helper with dot notation for keys [OK]
Common Mistakes:
  • Using arrow (->) instead of dot (.) in key
  • Using Config::get() without importing facade
  • Swapping file and key order
3. Given the config file config/mail.php contains:
return [
  'driver' => env('MAIL_DRIVER', 'smtp'),
  'host' => env('MAIL_HOST', 'smtp.mailtrap.io'),
];

And the environment variable MAIL_DRIVER is set to sendmail, what will config('mail.driver') return?
medium
A. "smtp"
B. "sendmail"
C. null
D. "smtp.mailtrap.io"

Solution

  1. Step 1: Understand env() usage in config

    The config uses env('MAIL_DRIVER', 'smtp'), which returns the environment variable if set, else 'smtp'.
  2. Step 2: Check environment variable value

    Since MAIL_DRIVER is set to 'sendmail', env('MAIL_DRIVER', 'smtp') returns 'sendmail'.
  3. Final Answer:

    "sendmail" -> Option B
  4. Quick Check:

    env() returns env var if set, else default [OK]
Hint: env() returns env var first, default second [OK]
Common Mistakes:
  • Assuming default value always returns
  • Confusing config key with env variable name
  • Ignoring environment variable settings
4. What is wrong with this code snippet to access a config value?
use Illuminate\Support\Facades\Config;

$value = Config::get('database.connections.mysql.host');
medium
A. The config key should use underscores instead of dots
B. The config key is too long and invalid
C. Nothing is wrong; this is correct syntax
D. The Config facade is not imported correctly

Solution

  1. Step 1: Check import statement

    The code imports Config facade correctly with use Illuminate\Support\Facades\Config;.
  2. Step 2: Verify config key syntax

    The key 'database.connections.mysql.host' uses correct dot notation to access nested config arrays.
  3. Final Answer:

    Nothing is wrong; this is correct syntax -> Option C
  4. Quick Check:

    Config::get() with dot keys and correct import is valid [OK]
Hint: Dot notation accesses nested config keys correctly [OK]
Common Mistakes:
  • Using underscores instead of dots in keys
  • Forgetting to import Config facade
  • Assuming key length causes errors
5. You want to create a new config file config/payment.php with a key gateway that uses an environment variable PAYMENT_GATEWAY defaulting to 'stripe'. How should you write the config file to access this value correctly in Laravel?
hard
A. env('PAYMENT_GATEWAY', 'stripe')];
B. config('PAYMENT_GATEWAY', 'stripe')];
C. 'PAYMENT_GATEWAY' ?? 'stripe'];
D. getenv('PAYMENT_GATEWAY', 'stripe')];

Solution

  1. Step 1: Use env() to read environment variables in config

    Laravel config files use env('VAR_NAME', 'default') to get env vars with defaults.
  2. Step 2: Write correct PHP array return

    Return an array with key 'gateway' set to env('PAYMENT_GATEWAY', 'stripe').
  3. Final Answer:

    <?php return ['gateway' => env('PAYMENT_GATEWAY', 'stripe')]; -> Option A
  4. Quick Check:

    Use env() in config files for env vars [OK]
Hint: Use env() with default inside config files [OK]
Common Mistakes:
  • Using config() inside config files instead of env()
  • Using getenv() without fallback default
  • Using PHP null coalescing incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard