-1

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?

JsWizard
  • 1,663
  • 3
  • 21
  • 48

2 Answers2

3

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');
Jerodev
  • 32,252
  • 11
  • 87
  • 108
1

config() helper or Config Facade is used to get the values from config dir.

Create a new file in config folder with name customerInfo.

return [
   'customer_name' => 'A name'
];

Now you can access the name

namespace App\Console\Commands;

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

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

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

      ***
   }

}
FULL STACK DEV
  • 15,207
  • 5
  • 46
  • 66