Error login in Laravel How to solve

0 votes

I have created a Login form called UserLogin.blade.php. And I have created a controller called UserLoginControl.php and model called login.php. Now I want to log into my system by using Username and Password which is in the database. But , after I enter Username and password and when clicking the Login button , it shows me this error -

(1/1) ErrorException

Undefined index: password

But , I have declared Username and Password in UserLoginController.php file.

How can I Fix this ??

Here is the UserLogin.blade.php ( View file ).

<!DOCTYPE html>
<html>
<head>
</head>

<body">

<form method="post" action="{{ route('UserLogin') }}">
{{ csrf_field() }}
Username : <input type="text" name="username"> <br><br>
Password : <br><input type="password" name="password" class="text"> <br><br>                
<input type="submit" name="login" value="Login"> <br>           
<small><a href="{{ route('first') }}">Return Home</a></small>
</form>

</center>

</body>
</html>

Here is the UserLoginController.php ( Controller file ).

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\login;
use Illuminate\Support\Facades\Auth;

class UserLoginController extends Controller
{

    public function index()
    {
        return view('UserLogin');
    }

    public function login(Request $request)
    {

        $this->validate($request, [
            'username' => 'required',
            'password' => 'required'
        ]);
        if (Auth::attempt(['username' => $request['username'], 'pw' => $request['password']])) {
            return redirect('RegView');
        }
        return redirect('UserLogin');
    }

}

Here is the login.php ( Model file ).

<?php

namespace App;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;

class login extends Model implements Authenticatable
{
    use \Illuminate\Auth\Authenticatable;
}

And I have written this in Auth.php file

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],

        'users' => [
            'driver' => 'eloquent',
            'model' => App\login::class,
        ],

Here is the Routes that I have created.

Route::any('/UserLogin', 'UserLoginController@index')->name('UserLogin');
Route::post('/UserLogin','UserLoginController@login');

Edureka please help me out with this error.

Thank You!!

Mar 26, 2020 in Laravel by kartik
• 37,510 points
1,744 views

1 answer to this question.

0 votes

Hey,

First check if your model login has a field password in the login database schema after that change this :

if (Auth::attempt(['username' => $request->get('username'), 'pw' => $request->get('password')])) {
    return redirect('RegView');
}

to:

if (Auth::attempt(['username' => $request->get('username'), 'password' => $request->get('password')])) {
    return redirect('RegView');
}

Also go to your app/Http/Auth/LoginController.php and add this :

  /**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function username()
    {
        return 'username';
    }

and finally change this :

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],

        'users' => [
            'driver' => 'eloquent',
            'model' => App\login::class,
        ],

to this :

'providers' => [

        'users' => [
            'driver' => 'eloquent',
            'model' => App\login::class,
        ],

check if there is any errors if the page refresh :

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

Hope this works fine

Thank you!!

answered Mar 26, 2020 by Niroj
• 82,720 points

Related Questions In Laravel

0 votes
1 answer

How to use withErrors with Exception error messages in Laravel?

Hello @kartik, Try this: return Redirect::to('admin/users/create') ...READ MORE

answered Oct 28, 2020 in Laravel by Niroj
• 82,720 points
780 views
0 votes
1 answer

How to redirect to login if user is not authenticated in laravel?

Hello @kartik, Use middleware for this purpose and ...READ MORE

answered Oct 28, 2020 in Laravel by Niroj
• 82,720 points
1,789 views
0 votes
1 answer

How to make auto login after registration in laravel?

Hello, You can try to login the user ...READ MORE

answered Nov 2, 2020 in Laravel by Niroj
• 82,720 points
924 views
0 votes
1 answer

How to redirect to Login if user not logged in Laravel?

Hello @kartik, You can use it in middleware. ...READ MORE

answered Dec 4, 2020 in Laravel by Niroj
• 82,720 points
2,310 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,720 points
7,117 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,720 points
814 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,720 points
632 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,720 points
7,538 views
0 votes
2 answers

How to solve expected response code 220 but got code “”, with message “” in Laravel?

This problem can generally occur when you ...READ MORE

answered Dec 16, 2020 in Laravel by Gitika
• 65,870 points
14,598 views
0 votes
1 answer

How to give custom field name in laravel form validation error message?

Hello @kartik, You can specify custom error message ...READ MORE

answered Sep 30, 2020 in Laravel by Niroj
• 82,720 points
1,237 views