redirect()->back() return with variable

Asked

Viewed 877 times

0

I have this form here

<form action="{{ route('usuario.update') }}" method="post">
     <input id="token" type="hidden" value="{{ csrf_token() }}" name="_token"/>
     <input type="hidden" value="{{ $usuario }}" name="user">
     <input type="hidden" value="{{ $usuario->id }}" name="id">
      @if( \Auth::id() == $usuario->id )
         <input type="hidden" value="S" name="sn_atual">
      @else
         <input type="hidden" value="N" name="sn_atual">
      @endif
      <div class="form-group col-lg-10 {{ $errors->has('nome') ? ' has-error' : '' }}">
      <label for="nome">Nome</label>
      <input id="nome" name="nome" class="form-control " required value="{{ $usuario->name }}"/>
      @if ($errors->has('nome'))
       <span class="help-block">
      <strong>{{ $errors->first('nome') }}</strong>
      </span>
      @endif
    </div>
    <div class="form-group col-lg-5 {{ $errors->has('email') ? ' has-error' : '' }}">
    <label for="email">E-mail</label>
    <input id="email" name="email" type="email" class="form-control" required required value="{{ $usuario->email }}"/>
    @if ($errors->has('email'))
      <span class="help-block">
        <strong>{{ $errors->first('email') }}</strong>
      </span>
     @endif
   </div>
    <div class="row"></div>
    <div class="form-group col-lg-3">
     <label>
       <input type="checkbox" value="S" name="sn_ativo" checked >Ativo
     </label>
    </div>
    @if( Auth::id() == $usuario->id )
    <div class="form-group col-lg-3 {{ $errors->has('pwd') ? ' has-error' : '' }}">
    <input  name="password" id="senha" class="form-control" placeholder="Senha" required />
    @if ($errors->has('pwd'))
      <span class="help-block">
        <strong>{{ $errors->first('pwd') }}</strong>
      </span>
   @endif
   </div>
   <div class="form-group col-lg-3">
   <input name="password1" id="senha" class="form-control" placeholder="Repita a Senha" required />
   </div>
   @else
   <div class="form-group col-lg-3">
   <input type="hidden" name="password" id="senha" class="form-control" placeholder="Senha" required value="12345678"/>
   </div>
   <div class="form-group col-lg-3">
   <input type="hidden" name="password1" id="senha" class="form-control" placeholder="Repita a Senha" required value="12345678"/>
   </div>
   @endif
    <div class="row"></div>
    <div class="col-lg-3">
    <button type="submit" class="btn btn-success btn-salvar">Salvar</button>
    <a class="btn btn-default btn-cancelar">Voltar</a>
   </div>

I have my route inside the group:

Route::group(['as' => 'usuario.', 'prefix' => 'usuario'], function (){
    Route::post('edit',['as' => 'edit', 'uses' => 'UsuarioController@edit']);
    Route::get('edit',['as' => 'edit', 'uses' => 'UsuarioController@edit']);
});

Within the group, my route generates so in the url:

http://meuapp/usuario/edit

When trying to change the back returns using get, but not all variables return successfully, for example:

The user object, gives problem

Trying to get Property of non-object (View:

How do I know?

I think it’s because of this line here:

at Phpengine->evaluatePath(’D: wamp www restaurant Storage framework views/40b090229a7702d4db310979456e280426f93706.php', array('__env' => Object(Factory), 'app' => Object(Application), 'errors' => Object(Viewerrorbag), 'user' => null)) in Compilerengine.php line

User comes null when back

'usuario' => null

Any idea?

[EDIT 1]

My controller I’m trying to do like this:

public function update( Request $request ){
        $nome      = $request->get('nome');
        $email     = $request->get('email');
        $password  = $request->get('password');
        $password1 = $request->get('password1');
        $ativo     = $request->get('sn_ativo');
        $senha     = $request->get('sn_atual');
        $id = $request->get('id');

        $usuario = User::find( $id );
        if( !is_null( $ativo ) ){
            $ativo = 'S';
        }else{
            $ativo = 'N';
        }

        $validator = Validator::make(
            [    'nome'   => $nome
                ,'pwd'    => $password
                ,'pwd1'   => $password1
                ,'email'  => $email
            ],
            [
                'nome'  => 'required|min:6'
                ,'pwd'   => 'required|min:8:confirmed'
                ,'pwd1'   => 'required|min:8'
                ,'email' => 'required'
            ],
            [
                'required' => ':attribute é obrigatório'
               ,'nome.min' => ':attribute tem que no mínimo 6 caracteres'
               ,'pwd.min' => ':attribute tem que no mínimo 8 caracteres'
            ]
        );
        if( $validator->fails() ){
            return redirect()->back()->withErrors( $validator )->withInput()->with( 'usuario', $usuario );
        }else{

            $usuario->name           = $nome;
            $usuario->email          = $email;
            $usuario->password       = Hash::make( $password );
            $usuario->sn_ativo       = $ativo;
            $usuario->sn_senha_atual = $senha;
            if( $usuario->save() ){
                //return response()->json( array( 'success' => 1 ) );
                return redirect()->route('usuario.index');
            }else{
                //return response()->json( array( 'success' => 0 ) );
                return redirect()->back()->with( 'exception', 'Um problema ocorreu' );
            }

            //return redirect()->route('emp.index');

        }
    }

1 answer

0

with will send key 'user' value ' $user' by Session, then you can recover so in Blade:

@if (session('usuario'))
        {{ session('usuario') }}
@endif

Browser other questions tagged

You are not signed in. Login or sign up in order to post.