英文:
Unable to access auth guard user for custom authenticated user
问题
I have created an authentication guard for staff in config/auth:
'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'staff' => [
        'driver' => 'session',
        'provider' => 'staff',
    ],
],
I have tried to access properties of logged in staff using:
{{ Auth::guard('staff')->user()->surname }}
but I get an error:
"Attempt to read property 'surname' on null."
web.php:
Route::middleware('staff')->group(function () {
    Route::get('staffdashboard', function () {
        return view('staff.staffdashboard');
    })->name('staffdashboard');
});
英文:
I have created an authentication guard for staff in config/auth
'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'staff' => [
            'driver' => 'session',
            'provider' => 'staff',
        ],
    ],
I have tried to access properties of logged in staff using
{{ Auth::guard('staff')->user()->surname}}
but i get error
Attempt to read property "surname" on null.
web.php
Route::middleware('staff')->group(function () {
Route::get('staffdashboard', function () {
    return view('staff.staffdashboard');
})->name('staffdashboard');
});
答案1
得分: 1
将中间件名称从staff更改为auth:staff,而不是web.php文件中的staff。
Route::middleware('auth:staff')->group(function () {
  Route::get('staffdashboard', function () {
     return view('staff.staffdashboard');
  })->name('staffdashboard');
});
英文:
Just change the middleware name to auth:staff instead of staff in your web.php file
Route::middleware('auth:staff')->group(function () {
  Route::get('staffdashboard', function () {
     return view('staff.staffdashboard');
  })->name('staffdashboard');
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论