vendredi 15 mai 2020

redirect with flash message not working when calling Artisan::call('config:cache')

I am using Laravel 5.8, I want to edit the .env file and for the changes to take effect immediately so I wrote function:

protected function updateDotEnv($configName, $key, $newValue, $delim='')
{

    $path = base_path('.env');
    // get old value from current env
    $oldValue = config($configName.'.'.$key);
    // was there any change?
    if ($oldValue === $newValue) {
        return;
    }
    // rewrite file content with changed data
    if (file_exists($path)) {
        file_put_contents(
            $path, str_replace(
                $key.'='.$delim.$oldValue.$delim,
                $key.'='.$delim.$newValue.$delim,
                file_get_contents($path)
            )
        );
        \Artisan::call('config:cache');
    }
}

then I used this function as follow:

public function updenv(Request $request){
    $from = $request->time_from;
    $to = $request->time_to;
    $this->updateDotEnv('adminconfig', 'start', $from, '');
    $this->updateDotEnv('adminconfig', 'endin', $to, '');
    return redirect()->back()->with('success', 'thank you');
}

then when using session()->get('success') I get null, but, if I re-wrote the function as follow:

public function updenv(Request $request){
    return redirect()->back()->with('success', 'thank you');
    $from = $request->time_from;
    $to = $request->time_to;
    $this->updateDotEnv('adminconfig', 'start', $from, '');
    $this->updateDotEnv('adminconfig', 'endin', $to, '');
}

I get the desired output, I guesss it is something related to \Artisan::call('config:cache'); in the updateDotEnv function but I do not know how to fix it.

would you please help me how to fix it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire