
在 Laravel 8 API 开发中,默认情况下 Auth::attempt 方法仅适用于从 users 表进行用户认证。然而,在某些应用场景下,我们需要从不同的表中认证不同类型的用户,例如从 students 表认证学生,从 teachers 表认证教师。本文将详细介绍如何在 Laravel 8 API 中实现这种多表用户认证。
首先,我们需要为每个用户类型创建对应的模型。假设我们有 Student 和 Teacher 两个模型,它们分别对应 students 和 teachers 表。
// app/Models/Student.php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Student extends Authenticatable
{
    use Notifiable;
    protected $table = 'students';
    protected $fillable = [
        'name', 'email', 'password',
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];
}
// app/Models/Teacher.php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Teacher extends Authenticatable
{
    use Notifiable;
    protected $table = 'teachers';
    protected $fillable = [
        'name', 'email', 'password',
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];
}请注意,这两个模型都继承自 Illuminate\Foundation\Auth\User,并实现了 Notifiable trait。$table 属性用于指定模型对应的数据库表。$fillable 属性定义了可以批量赋值的字段,$hidden 属性定义了需要隐藏的字段。
接下来,我们需要创建自定义的身份验证守卫。在 config/auth.php 文件中,添加新的 guards 配置。
'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],
    'student' => [
        'driver' => 'jwt', // 或者其他适合 API 的 driver,例如 token
        'provider' => 'students',
    ],
    'teacher' => [
        'driver' => 'jwt', // 或者其他适合 API 的 driver,例如 token
        'provider' => 'teachers',
    ],
],这里我们添加了 student 和 teacher 两个新的守卫。driver 可以选择适合 API 的驱动,例如 jwt 或 token。provider 属性指向了用户提供器。
然后,我们需要创建自定义的用户提供器。在 config/auth.php 文件中,添加新的 providers 配置。
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
    ],
    'students' => [
        'driver' => 'eloquent',
        'model' => App\Models\Student::class,
    ],
    'teachers' => [
        'driver' => 'eloquent',
        'model' => App\Models\Teacher::class,
    ],
],这里我们添加了 students 和 teachers 两个新的提供器。driver 属性设置为 eloquent,model 属性指向了对应的用户模型。
现在,我们可以在控制器中使用自定义的身份验证守卫进行认证。
use Illuminate\Support\Facades\Auth;
use App\Models\Student;
use App\Models\Teacher;
public function studentLogin(Request $request)
{
    $credentials = $request->only('email', 'password');
    if (Auth::guard('student')->attempt($credentials)) {
        $student = Auth::guard('student')->user();
        $token = $student->createToken('student-token')->plainTextToken; // 使用 Sanctum 生成 Token
        return response()->json(['token' => $token, 'user' => $student]);
    }
    return response()->json(['message' => 'Invalid credentials'], 401);
}
public function teacherLogin(Request $request)
{
    $credentials = $request->only('email', 'password');
    if (Auth::guard('teacher')->attempt($credentials)) {
        $teacher = Auth::guard('teacher')->user();
        $token = $teacher->createToken('teacher-token')->plainTextToken; // 使用 Sanctum 生成 Token
        return response()->json(['token' => $token, 'user' => $teacher]);
    }
    return response()->json(['message' => 'Invalid credentials'], 401);
}在这个例子中,我们使用了 Auth::guard() 方法来指定使用哪个身份验证守卫。attempt() 方法会根据指定的守卫和用户提供器来验证用户凭据。如果认证成功,我们可以使用 Sanctum 生成 API token 并返回给客户端。
通过自定义身份验证守卫和用户提供器,我们可以在 Laravel 8 API 中实现多表用户认证。这种方法可以灵活地处理不同类型的用户,并为每个用户类型提供独立的认证机制。希望本文能够帮助你更好地理解和应用 Laravel 的身份验证功能。
以上就是Laravel 8 API 多表用户认证教程的详细内容,更多请关注php中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号