Is there any way to save a variable somewhere other than in the BD in Laravel 5.5?

0

I'm trying something half crazy. The use case is that every 2 years the Board of Directors changes, so this year it will change to the XXXVII Council (It is currently in the XXXVI). It made me absurd to create a table just to save the number of advice then it occurred to me to save the number in a variable created in the controller and edit it with x-editable. Everything is fine except that it does not save the variable. Here is my code:

Controller Code

//Declaro la variable
protected $consejo;

// Inicializo la viariable para mandarla a la vista
public function editDirectors() 
{
    $numConsejo = $this->consejo;
    return view('board.edit_directors', compact('numConsejo'));

}
//Se supone que guardo la variable
public function uploadNumber(Request $request)
{
    $this->consejo = $request->value;
    $numConsejo = $this->consejo;
    return $numConsejo;
}

Code in the template

<a href="#" id="num-consejo" data-type="text" 
                                 data-pk="1" 
                                 data-url="{{url('/consejo-directivo/editar/numero-consejo')}}"
                                 data-value="{{$numConsejo}}" 
                                 data-name="numConsejo" 
                                 data-title="Número de Consejo Directivo">{{$numConsejo}}</a>
    Consejo Directivo</h3>

I initialize the x-editable

//Se inicializa el x-editable para modificar el número del comité
$('#num-consejo').editable({
    error: function(response, newValue) 
    {
        if(response.status === 500) {
            return 'Parece que hubo un error al guardar. Favor de reportarlo a: [email protected]';
        } else {
            return 'Parece que hubo un error al guardar. Favor de reportarlo a: [email protected]';
        }
    }
});

And the route

Route::put('/consejo-directivo/editar/numero-consejo' , 'DirectorController@uploadNumber');//Ruta para guardar el número de consejo

I am more than sure that I am saving the variable but I can not find any solution because I already debug the console and the Request arrives at the controller and makes the return with the value that I entered in the x-editable but when I gave it F5 it was not saved and when I do a var_dump () or dd () in the editDirectors () method I get that the $ advice variable is null.

I am grateful for the help and attention if you came to read up to here: 3

Modifications

I made a file in the config folder to store the value there (config / board.php) and added the following code:

return [
'director' => [
    'number' => 'XXXVI'
    ]
];

Often I modified the controller code:

//Aquí traigo el valor de config/board.php y lo mando al template, funciona :D
public function editDirectors() 
{
    $numConsejo = Config::get('board.director.number');
    return view('board.edit_directors', compact('directors', 'numConsejo'));
}

//Aquí se supone que guardo la variable en el archivo config/board.php pero no guarda :(
public function uploadNumber(Request $request)
{
    $numConsejo = $request->value;
    Config::set('board.director.number', $numConsejo);
    dd(Config::get('board.director.number'));
    //return $numConsejo;
}

I had to run the command:

php artisan config:cache

This is so that the config / board.php file could be displayed but it seems that each time the value of that file is modified, I need to run that command.

Any ideas? I would greatly appreciate the help: 3

    
asked by RalphVB 06.01.2018 в 04:40
source

1 answer

0

Solution

I think what I did is bad practice but it covers my need.

Taken from: link

In the .env file I added:

BOARD_NUMBER = XXXVI

And this is my code:

//Mando a llamar al valor que tiene el BOARD_NUMBER en el .env y lo mando al template.
public function editDirectors() 
{
    $numConsejo = env('BOARD_NUMBER');
    return view('board.edit_directors', compact('directors', 'numConsejo'));
}

//Abro el archivo .env y sobreescribo el viejo valor con el nuevo
protected function uploadNumber(Request $request)
{
    $numConsejo = $request->value;
    $envFile = app()->environmentFilePath();
    $str = file_get_contents($envFile);

    $oldValue = env('BOARD_NUMBER');

    $str = str_replace("BOARD_NUMBER={$oldValue}", "BOARD_NUMBER={$numConsejo}\n", $str);

    $fp = fopen($envFile, 'w');
    fwrite($fp, $str);
    fclose($fp);
}

In the same link there is another solution where the command "php artisan config: cache" is executed from the same code but I doubt it works because I will upload the application to a shared server.

Greetings and I hope this serves someone:)

    
answered by 06.01.2018 в 22:20