0

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');

Here is my logins table.

enter image description here

  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – M. Eriksson Aug 08 '17 at 15:25
  • request method is an object not an array. Thus you will get password by $request->password – Web Artisan Aug 08 '17 at 15:27
  • @BikashP - Still , gives me that Undefined index: password error.. How can I Fix this ?? – Kistlak Rajapaksha Aug 08 '17 at 15:40

4 Answers4

3

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

if you really want to use pw instead of password look at this answer : https://stackoverflow.com/a/39382427/4369087

yoeunes
  • 2,927
  • 2
  • 15
  • 26
1

You should access posted data using the get method on $request, rather than using the array syntax.

if (Auth::attempt(['username' => $request->get('username'), 'pw' => $request->get('password')])) {
    return redirect('RegView');
}
Jono20201
  • 3,215
  • 3
  • 20
  • 33
1

According to the docs, there are two ways of doing this (wrapped just for clarity in this post):

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

or

if (Auth::attempt([
    'username' => $request->input('username'),
    'pw' => $request->input('password')
])) {
        return redirect('RegView');
    }
Elliot
  • 1,457
  • 13
  • 40
1

In your controller:

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

In your model:

protected $fillable = [
    'username', 'password'
];
the_lorem_ipsum_guy
  • 452
  • 1
  • 12
  • 27
  • When I use this , page is refreshed.. Nothing happened.. How to Fix this ?? – Kistlak Rajapaksha Aug 08 '17 at 15:57
  • can you try to debug it by using `dd($request->all());` in your controller before the if statement and check if it has a data from your form? It will output a something like this `array:3 [▼ "_token" => "ytBDTz45zi1ac1oOgwH83XFEszcpMpsc0MlZ3A6l" "username" => "test@gmail.com" "password" => "test" ]` – the_lorem_ipsum_guy Aug 08 '17 at 16:05
  • Yh , It shows me correct username and password like this - array:4 [▼ "_token" => "TixaQBCmgwXoCDFs5owm9r4hexRPhzfaxFBgqRzR" "username" => "user" "password" => "user" "login" => "Login" ] – Kistlak Rajapaksha Aug 08 '17 at 16:08
  • `if (auth()->attempt(['email' => $request->email, 'password' => $request->password])) { return redirect()->route('dashboard'); } else { return back()->withErrors([ 'message' => 'Please check your credentials and try again.' ]); }` try it something like this and try to add `dd()` in if statement if where your code is executing. – the_lorem_ipsum_guy Aug 08 '17 at 16:11
  • But , there is no column called email in database.. How can I Fix this ?? – Kistlak Rajapaksha Aug 08 '17 at 16:16