Laravel Auth ::尝试()总是假的

时间:2018-02-23 01:08:04

标签: php laravel

首先,我已经看到了这个主题(和许多其他主题):

Laravel Auth::attempt() always false?

Laravel 5 Auth:attempt() always returns false

但我无法登录我的应用程序。我使用Laravel 5.6 ,我的表名为oc_users

DB::table('oc_users')->insert(
[
    'email'     => 'myemail@gmail.com',
    'pwd'       => bcrypt('123456'),
    'active'    => true
]);

我去了La​​ravel文件>应用> User.php并更改为:

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $table = 'oc_users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['email', 'pwd'];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = ['pwd', 'remember_token'];
}

执行登录的代码:

public function form_login(Request $request)
{   
    if($request->filled('email') == FALSE || $request->filled('pwd') == FALSE)
        return Redirect::back()->withErrors('Fill the credentials');

    if (Auth::attempt(['email' => $request->email, 'pwd' => $request->pwd]))
        return redirect()->intended('app');

    return Redirect::back()->withErrors('Email or password wrong');       
}

我也去过App>文件。配置> Auth.php试图弄清楚我是否需​​要改变一个表,但它似乎并不存在于Laravel 5.6中

2 个答案:

答案 0 :(得分:3)

根据我之前的经验,auth :: attempt()函数默认检查password列。

为了让你的代码有效,你可以试试这个。

在您的用户模型中添加:

public function getAuthPassword() {
    return $this->pwd;
}

此函数将覆盖Authenticatable.php

中的默认getAuthPassword()函数

答案 1 :(得分:0)

Laravel 现在使用 bcrypt

class User extends Model {

    public function setPasswordAttribute($value) {
         $this->attributes['password'] = bcrypt($value);
    }

}