J'ai une application où j'ai créé une nouvelle garde appelée residents
qui est la copie exacte de la classe User
classée avec le Laravel. Mais quand je change le gardien, il commence à montrer cette erreur:Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given, called in ...\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php on line 125
J'ai essayé de googler pour la solution et j'ai lu beaucoup mais rien n'a fonctionné.
La version Laravel est de 5,8
auth.php
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'residents',
'hash' => false,
],
],
'providers' => [
'residents' => [
'driver' => 'eloquent',
'model' => App\ApiModels\Resident::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
app\apimodels\résident.php
namespace App\ApiModels;
use \Illuminate\Notifications\Notifiable;
use \Illuminate\Contracts\Auth\MustVerifyEmail;
use \Illuminate\Foundation\Auth\User as Authenticatable;
class Resident extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* @Author : Parminder Singh
* @Last modified : 09, August 2019
*
* @Function name : _login
* @Description : Function to authenticate the user
* @Parameters : $request as $object
*
* @Method :
* @Returns : raw data
* @Return type : array
*/
public function _login($request)
{
if(\Auth::attempt([
'email' => $request->email,
'password' => $request->password
]))
{
return [
'success' => true
];
}
else
{
return [
'success' => false
];
}
}
}
route\api.php
Route::get('/login', 'Api\ResidentsController@login');
API\RESIDENTSCONTROLLER.PHP
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
/**
* Models
*/
use App\ApiModels\Resident;
class ResidentsController extends Controller
{
public function __construct()
{
/**
* Middleware(s)
*/
$this->middleware('auth:api', [
'except' => [
'login',
'logout'
]
]);
}
public function login(Request $request)
{
...
return response()->json([
'login_id' => $request->login_id,
'password' => $request->password
]);
}
}
Le problème se produit dans votre config/auth.php
. Votre web
Guard utilise le fournisseur users
qui n'est pas déclaré dans votre tableau providers
. Vous avez 2 options pour résoudre ce problème (choisissez-en un):
users
web
, supprimez-le plutôt que votre garde par défaut à api
:'defaults' => [
'guard' => 'api',
'passwords' => 'residents',
],
'guards' => [
'api' => [
'driver' => 'token',
'provider' => 'residents',
'hash' => false,
],
],
'providers' => [
'residents' => [
'driver' => 'eloquent',
'model' => App\ApiModels\Resident::class,
],
],
'passwords' => [
'residents' => [
'provider' => 'residents',
'table' => 'password_resets',
'expire' => 60,
],
],