Laravel 5.4 if语句带有auth条件

时间:2018-09-17 10:22:22

标签: laravel laravel-5.4

问题在于它给出了一个错误。

未定义的属性:Illuminate \ Auth \ SessionGuard :: $ confirmed

这是我的代码:

if (Auth::guard('pharmacy')->confirmed == 1) {


    if (Auth::guard('pharmacy')->status == 'Approved') {


        return redirect()->intended(route('pharmacy.dashboard'));

      }

    }

2 个答案:

答案 0 :(得分:0)

您应该改为这样做:

if (Auth::guard('pharmacy')->attempt(['email' => $email, 'password' => $password])) {
    if (Auth::user()->confirmed == 1 && Auth::user()->status == 'Approved') {
        return redirect()->intended(route('pharmacy.dashboard'));
    }
    //You might want to do something else here for those that fail either the confirmed or approved check.
}

首先,您应该针对pharmacy保护实例对用户进行身份验证。只有在用户通过身份验证后,您才可以访问他/她的attributes ...在您的情况下... confirmedstatus

答案 1 :(得分:0)

确保您的表具有字段confirmed,并且您已经正确注册了保护人员以将该表用作提供程序。

后卫config/auth.php

'guards' => [

   // ...

   'pharmacy' => [
      'driver' => 'session',
      'provider' => 'pharmacies',
   ],
],

迁移

class CreatePharmaciesTable extends Migration
{
   public function up()
   {
      Schema::create('pharmacies', function (Blueprint $table) {
            // ...
            $table->boolean('confirmed')->default(false);
      });
   }

   // ...

}