如何在laravel中更改api身份验证模型而不是使用默认用户模型

我有两个模型client和user。对于Web登录,我已经将用户模型用作超级管理员。但我希望通过api请求将客户端模型用于移动登录。

api.php

Route::group(['middleware' => 'auth:api'],function() {
    Route::resource('communities','communityAPIController');
    Route::resource('communities','communityAPIController');
    Route::resource('clients','ClientAPIController');
});

Config / auth.php

'guards' => [
        'web' => [
            'driver' => 'session','provider' => 'users',],'api' => [
            'driver' => 'token','provider' => 'clients','hash' => false,'providers' => [
        'users' => [
            'driver' => 'eloquent','model' => App\User::class,'clients' => [
             'driver' => 'eloquent','model' => \App\Models\Client::class,

我的客户模型是这样的。

<?php

namespace App\Models;

use Eloquent as Model;

class Client extends Model
{

    public $table = 'clients';



    public $fillable = [
        'name','phone','house_no','type','is_approved','community_id'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer','name' => 'string','phone' => 'string','house_no' => 'string','type' => 'integer','is_approved' => 'boolean','community_id' => 'integer'
    ];



}

我想对auth:api使用客户端模型而不是用户模型。请帮我

xiaoxia20008 回答:如何在laravel中更改api身份验证模型而不是使用默认用户模型

尝试一下



'guards' => [
        'web' => [
            'driver' => 'session','provider' => 'users',],'api' => [
            'driver' => 'token','provider' => 'clients','providers' => [
        'users' => [
            'driver' => 'eloquent','model' => App\User::class,'clients' => [
             'driver' => 'eloquent','model' => App\Models\Client::class,'table'=>'clients'
         ],
,
        try This 

         'guards' => [
                'web' => [
                    'driver' => 'session','api-clients' => [
                    'driver' => 'token','table' => 'users','clients' => [
            'driver' => 'eloquent','table' => 'clients',also make sure to mentions drivers to be used in controller's constructor like this 
    public function __construct()
        {


        auth()->shouldUse('api-clients');


        }
本文链接:https://www.f2er.com/3154679.html

大家都在问