简体   繁体   中英

Laravel - Show error on the same page if login credentials are wrong

I am new to Laravel and i am working on login page in which i want throw error below input tags if login credentials are wrong. I have written some code and everything is working fine but errors are not showing.

Have a look at what I have done.

In Controller:

public function login(Request $req) {

    $email = $req->input('email');

    $password = $req->input('password');

    $checkLogin = DB::table('admin')->where(['email'=>$email,'password'=>$password])->get();

    if(count($checkLogin) > 0) {
        echo "Login Successfull";
    }
    else {
        // echo "Login Failed!";
        return Redirect::route('admin-login')->with();
    }
}

In View:

<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>

@if ($errors->has('email'))

<span class="help-block">

<strong>{{ $errors->first('email') }}</strong>

</span>

@endif

In Route:

Route::post('admin-login-result','AdminLoginController@login')->name('admin-log');

Thanks

Try this one in your else block

Redirect::back()->withErrors(['msg', 'The Message']);

In view file use like this

@if($errors->any())
  <h4>{{$errors->first()}}</h4>
@endif

Refer this link for more details https://itsolutionstuff.com/post/laravel-53-form-input-validation-rules-example-with-demoexample.html

您可以使用登录用户

Auth::login($user);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM