Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';t使用Laravel 5.1上创建的模型进行验证_Php_Authentication_Laravel 5.1 - Fatal编程技术网

Php Can';t使用Laravel 5.1上创建的模型进行验证

Php Can';t使用Laravel 5.1上创建的模型进行验证,php,authentication,laravel-5.1,Php,Authentication,Laravel 5.1,在带有注释“这不起作用”的代码中,我无法授权 我获取user对象并使用Auth::login($user),但它不起作用 此时用户在数据库中创建 public function login(Request $request) { if(Auth::check()) { return redirect('home'); } if($request->isMethod('post')) { if($request->has('us

在带有注释“这不起作用”的代码中,我无法授权

我获取user对象并使用
Auth::login($user)
,但它不起作用

此时用户在数据库中创建

  public function login(Request $request) 
  {
    if(Auth::check()) {
      return redirect('home');
    }
    if($request->isMethod('post')) {
      if($request->has('username') && $request->has('password')) {
        $inputs = $request->except('_token');
        if(!Auth::attempt(['username' => $inputs['username'], 'password' => $inputs['password']])) {
          $user = $this->registerUser($inputs);
          if($user) {
            Auth::login($user);
          }
        } 
      } 
      return (Auth::check()) ? redirect()->route('home') : redirect()->back()->with('form_error', true);
    }
    return view('pages.auth.login');
  }
php可能这里出了什么问题

namespace App\Models\system;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;
    protected $primaryKey = 'uid';
    protected $table = 'eltk.dbo.system_users';
    protected $guarded = [];
    protected $hidden = ['password', 'remember_token'];
}

让我们一步一步地开始

  • 为什么要在只需要处理登录逻辑的方法上创建用户
  • 你的数据库名是什么?是eltk还是dbo还是什么?在config/database.php中的“database”或.env下定义它,并在模型中使用表名,而不是整个表的名称,这是不必要的
  • 在登录方法中,您需要如下内容:

    public function processLogin(Request $request){
    if(Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password'), 'active' => 1])){
        return redirect('/');
    }else{
        return redirect('login')->withMessage('User with this email and/or password does not exist or your account is not active.');
    }
    
    }


  • 顺便说一句,当你在if-else语句中登录用户之后,你需要在某个路径上重定向他。

    wow,这真是一团糟。也许考虑把你的代码整理成其他函数或方法。这通常可以帮助您找到问题谢谢您的建议:)我对代码进行了一些重构,但仍然不起作用。若在开始时未创建用户,则他无法进行身份验证,但在第二次输入数据后-用户身份验证成功。我发现一个问题,我使用自定义生成的主键(“uid”列),当我创建用户时,系统看不到动态生成的uid:(但在重新发送请求后,当用户已经创建时,一切正常!您首先需要在控制器中创建两种方法。一种用于显示用户可以注册的视图的方法,另一种用于将用户保存到数据库中,然后您的登录方法将起作用:)