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 in Laravel by kartik
• 4,560 points
25 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 by Niroj
• 8,620 points

Related Questions In Laravel

0 votes
1 answer

How to pass data through URL and access through controller in Laravel?

Hello, You can  first refer how to  Create controller through ...READ MORE

answered Mar 18 in Laravel by Niroj
• 8,620 points
43 views
0 votes
1 answer

How to identify wheather the request is HTTP GET or HTTP POST in Laravel?

Hey, In order to identify the type of ...READ MORE

answered Mar 19 in Laravel by Niroj
• 8,620 points
22 views
0 votes
1 answer

How to mock a static facade methods in Laravel?

Hey, Facades provide a "static" interface to classes ...READ MORE

answered Mar 19 in Laravel by Niroj
• 8,620 points
26 views
0 votes
1 answer

How to delete or clear caching in Laravel?

Hey @kartik, There are many commands to clear ...READ MORE

answered Mar 20 in Laravel by Niroj
• 8,620 points
31 views
0 votes
1 answer

How to make anchor tag with routing using Laravel?

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

answered Mar 18 in Laravel by Niroj
• 8,620 points
45 views
0 votes
1 answer

What is redirection in Laravel?

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

answered Mar 18 in Laravel by Niroj
• 8,620 points
22 views
0 votes
1 answer

How to install Laravel via composer?

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

answered Mar 23 in Laravel by Niroj
• 8,620 points
29 views
0 votes
1 answer

What are named routes in Laravel?

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

answered Mar 23 in Laravel by Niroj
• 8,620 points
21 views
0 votes
1 answer

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

Hii @kartik, if you are using Swift Mailer: ...READ MORE

answered 4 days ago in Laravel by Niroj
• 8,620 points
22 views
0 votes
1 answer

How to pass URL param in Laravel?

Hello, First you have to go to routes ...READ MORE

answered Mar 18 in Laravel by Niroj
• 8,620 points
33 views