laravellaravel-scheduler

Using the config file in Laravel Schedule Job


Can I load the config file when the scheduling job starts?

I tried to use a local variable customerName in Schedule Class and it already defined in the Config folder as named customerInfo.

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Config;

class Checkout extends Command
{
   ***
   public function handle()
   { 
      ***

      $customerName = Config::get('customerInfo.customer_name'); //test code
      \Log::info($customerName); // for error check in log file

      ***
   }

}

but it was not worked.

Do I have to declare it in the constructor or have to use '\' as '\Config' even if already declared alias as use Config;?

What is the best simple solution to use custom variable in Config when schedule job is running start?


Solution

  • You are getting this error because you have not defined in what namespace PHP can find the Config class.

    You need to either include the Config facade in your usings on top of the class:

    use Config;
    

    Or use the config helper function:

    config('customerInfo.customer_name');