duancutan4770 2017-08-08 15:22
浏览 41
已采纳

如何在Laravel中解决此登录错误

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

  • 写回答

4条回答 默认 最新

  • douzhi3779 2017-08-08 15:35
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题