Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 909
user9332352 Avatar asked Feb 16 '18 05:02

user9332352


2 Answers

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

like image 84
Sathishkumar Rakkiyasamy Avatar answered Sep 30 '22 00:09

Sathishkumar Rakkiyasamy


You can use for login users

Auth::login($user);
like image 29
keshav khandelwal Avatar answered Sep 29 '22 23:09

keshav khandelwal