【问题标题】:Session guard doesn't found未找到会话保护
【发布时间】:2019-01-01 10:17:17
【问题描述】:

我使用 laravel 并使用 mltiple auth 进行连接。 我的验证码

if (Auth::guard('patient')->attempt(['email' => $email, 'password' => $password])) {
            //if(Auth::attempt($request->only($field, 'password'))){
                //put session
                Session::put('key', $user->id);
                //change le status en ligne
                Patient::WHERE('id', Auth::patient()->id)->UPDATE(['status' => 1]);
                //redirect url
                return redirect('/users/profile')->with('success', 'Bienvenue '.$user->firstname.', vous êtes bien connecté');
            }else{
                return redirect('/users/login')->with('error', 'Email or username incorrect!');
            }

在 auth.php 中,我有

'guards' => [
    'user' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'patient' => [
        'driver' => 'session',
        'provider' => 'patients',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
   ],
],

执行后发现这个错误

BadMethodCallException 方法 Illuminate\Auth\SessionGuard::patient 不存在。

我的代码有什么问题?

【问题讨论】:

  • 显示来自 auth.php 的提供者部分
  • @Ts8060 [code] 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\Users::class, ], '患者' => [ '司机' => '雄辩的', '模型' => App\Models\Patient::class, ], ],
  • 您的 Patient 类是否扩展了 Authenticatable 类?
  • @Ts8060 这是真正的课程
  • 你试图在哪里执行这个保护代码?在控制器的哪个部分或其他任何地方?

标签: php laravel session laravel-5 eloquent


【解决方案1】:

您的代码有什么问题是SessionGuard 上没有名为patient 的方法。

Auth::patient()Auth::guard(null)->patient() ... 警卫上没有定义 patient 方法,也不会有。

Auth::user()Auth::guard(null)->user(),表示返回当前经过身份验证的用户。 user 并不意味着用户模型或配置文件中定义的任何名称。 user 是当前使用应用程序的人/实体的概念。正在使用它的 USER。

【讨论】:

  • 我使用这个 Patient::WHERE('id', Auth::guard('patient')->user()->id)->UPDATE(['status' => 1] );代码准备好了
【解决方案2】:
if (Auth::guard('patient')->attempt(['email' => $email, 'password' => $password])) {
        //if(Auth::attempt($request->only($field, 'password'))){
            //put session
            Session::put('key', $user->id);
            //change le status en ligne
            Patient::WHERE('id', Auth::guard('patient')->user()->id)->UPDATE(['status' => 1]);
            //redirect url
            return redirect('/users/profile')->with('success', 'Bienvenue '.$user->firstname.', vous êtes bien connecté');
        }else{
            return redirect('/users/login')->with('error', 'Email or username incorrect!');
        }

【讨论】:

    【解决方案3】:

    如果定义你自己的登录或注册,你得到了这种错误 坏方法调用异常 方法 Illuminate\Auth\SessionGuard::guard 不存在。 然后你应该导入或使用如下定义的会话类 使用 Illuminate\Support\Facades\Session; 你的函数应该定义为

    public function postSingIn(Request $request)
    {
       if(Auth::attempt(['email'=>$request['email'],['password'=>$request['password']]]))
       {
    
          return redirect()->action('BackendController@deshboard');
       }
       return redirect()->back();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 2015-06-18
      • 2020-10-11
      • 2014-03-12
      • 1970-01-01
      相关资源
      最近更新 更多