Change session variable value Laravel

0

I have a problem with how to change the session variable in Laravel. When the user logs in, some variables are set in the session, one of them is the user name. In a given form, there is the option to change the user data, if it changes the name, the value that was set in the session should be changed but I do not know how to do it.

session(
         [
          'id_usuario' => $usuario[0]->id_user,
          'usuario' => $usuario[0]->nome
         ]
);

How do I change the user field of the session array?

    
asked by anonymous 25.11.2017 / 05:14

1 answer

0

As you are doing, change the user name in the session. If you want to change / define only the name you can do:

session(['id_usuario' => 'novo nome do usuario aqui']);

If you are reading the form name you need to use $request->input and specifying the form field that has the name:

session(['id_usuario' => $request->input('nome')]);

The nome in this last example corresponds to the name attribute of the form field that has the name:

<form>
    ...
    <input type="text" name="nome"/>
    <!-- Corresponde a ------^  -->

Another alternative would be to use the put method:

$request->session()->put('id_usuario', $request->input('nome'));

Laravel Session Documentation

    
25.11.2017 / 11:19