如何解决Laravel中的此登录错误 [英] How To Solve This Login Error In Laravel

查看:100
本文介绍了如何解决Laravel中的此登录错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为UserLogin.blade.php的登录表单.我创建了一个名为UserLoginControl.php的控制器和一个名为login.php的模型.现在,我想使用数据库中的用户名"和密码"登录到我的系统.但是,在输入用户名和密码并单击登录"按钮后,它向我显示此错误-

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

(1/1) ErrorException

未定义索引:密码

但是,我已经在UserLoginController.php文件中声明了用户名和密码.

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

如何解决此问题?

这是UserLogin.blade.php(查看文件).

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>

这是UserLoginController.php(控制器文件).

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

}

这是login.php(模型文件).

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;
}

我已经在Auth.php文件中写了这个

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.

推荐答案

首先检查您的模型login在登录数据库模式中是否在字段password中,然后进行以下更改:

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

收件人:

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

还转到您的app/Http/Auth/LoginController.php并添加以下内容:

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

最后改变这个:

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

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

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

对此:

'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

如果您真的想使用pw而不是password,请查看以下答案: https://stackoverflow.com/a/39382427/4369087

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

这篇关于如何解决Laravel中的此登录错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆