Return message to user

Asked

Viewed 973 times

0

I would like if this error when logging in to return a message to the user

In my view login.blade.php is like this

div class="login-form">
    <form action="{{ route('logar') }}" method="post">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
        {{--<h2 class="text-center">Log in</h2>--}}
        <div class="text-center">
            <img src="{{ URL('img/logo_poraque.png') }}" class="img-rounded" alt="Cinque Terre" width="150">
        </div>

        <br />
        
        @if ($errors->has('msg'))
            <div style="text-align: center">
                <span class="help-block has-error" >
                      <strong>{{ $errors->first('msg') }}</strong>
                </span>
            </div>
        @endif
        
        <div class="form-group has-feedback {{ $errors->has('login') ? ' has-error' : '' }}">
            <input type="text" class="form-control" placeholder="Username" required="required" name="login" value="{{ old('login') }}">
            @if ($errors->has('login'))
                <span class="help-block">
                  <strong>{{ $errors->first('login') }}</strong>
                </span>
            @endif
        </div>
        <div class="form-group  has-feedback {{ $errors->has('senha') ? ' has-error' : '' }}">
            <input type="password" class="form-control" placeholder="Password" required="required" name="senha">
            @if ($errors->has('senha'))
                <span class="help-block">
                  <strong>{{ $errors->first('senha') }}</strong>
                </span>
            @endif
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-primary btn-block">Log in</button>
        </div>
       <!-- <div class="clearfix">
            <label class="pull-left checkbox-inline"><input type="checkbox"> Remember me</label>
            <a href="#" class="pull-right">Forgot Password?</a>
        </div>
        -->
    </form>

On my route web php. is like this:

Route::post( '/logar', [ 'as' => 'logar', 'uses' => 'UsuarioController@logar' ] );

In my Usuariocontroller.php is like this:

public function logar(Request $request){
        $login = $request->input( 'login' );
        $senha = $request->input( 'senha' );
        
            $usuario = Usuario::where( [
                ['usua_login', $login]
            ])->first();

            
              $user = Usuario::where( [
                  ['usua_login', $login],
                  ['usua_senha', sha1( $senha )]
              ])->first();

              if( $user !== null ){

                  Session::put([
                      'login' =>  $login,
                      'id'    =>  $user->usua_id
                  ]);
                  return redirect()->route('telaPrincipal');
              }else{
                  $msg = array(
                      "msg" => "Login ou senha não são válidos"
                  ); 
                  return redirect()->back()->withErrors( $msg )->withInput();
              }
      }

But when I error the password, it returns to the login and no message

What can it be?

If I give the print_r( ) in the controller it shows where it drops but does not return with the message in the view.

  • 1

    I think it may be in the Blade in the form that sends the errors tries to validate with @if($errors->any())

  • Just remember that the site snippet DOES NOT work when the code is not only HTML/CSS/JS, so there is no reason to use it in other situations. Use, for these cases, the code snippet tool, {}.

  • @Panda even so , came back nothing.

  • @Andersoncarloswoss I’m actually using in Blade.php which is like this: {{ }}

  • Maybe that’s not the kind of answer you’d like, but have you tried using auth ? Goes to the root of the project and creates with Artisan, "php Artisan make:auth" it will create the complete basic system of authentication already with the errors handled; then you edit the views it creates in Resources/views/auth and ready to go.

  • To write a code instead of a string {!! !!};

  • This code worked before. I don’t understand why it stopped. If at least this error

  • I prefer to create my own authentication

Show 3 more comments

1 answer

2

If you want to send any message, you can use Session Flash Data.

For example, before the Return from the login error, put this

$request->session()->flash('alert-danger', 'Login ou senha não são válidos.');
return redirect('/login');

To recover, put in login.blade.php that code below that will show the message with the inserted text.

@foreach (['danger', 'warning', 'success', 'info'] as $msg)
    @if(Session::has('alert-' . $msg))
        <div class="alert alert-{{ $msg }}" role="alert">
            {!! Session::get('alert-' . $msg) !!}
        </div>
    @endif
@endforeach

Basically we’re looping some types of alerts from bootstrap (Danger, Warning, Success focus), and checking if there is any session with data, if it shows the alert.

For a message of success could be something like that

$request->session()->flash('alert-success', 'Dados alterados com sucesso.');

I like to put this loop with the recovery of alerts on a separate page, for example partial/message-top.blade.php, and place a include of that page in a system header or pages that may display an alert.

@include('parciais.mensagem-topo')

Browser other questions tagged

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