ali_254 Posted August 9, 2022 Share Posted August 9, 2022 hi. I created a guard with the name "admin". 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Models\Admin::class, ], ], and login with method attempt: if(Auth::guard('admin')->attempt($credentials)) { $request->session()->regenerate(); return redirect()->route('admin.index'); } And I have assigned a Middlewareto the Route: Route::prefix('dashboard/admin/brand')->middleware(['auth:admin'])->group(function () { Route::get('/view', [BrandController::class, 'BrandView'])->name('all.brand'); } But when I want to access the properties of Guard Admin, it returns null: class BrandController extends Controller { public function __construct() { dd (Auth::guard('admin')->user()); //return null dd (Auth::guard('admin')->guest()); // return true } { What is the problem? Quote Link to comment Share on other sites More sharing options...
maxxd Posted August 9, 2022 Share Posted August 9, 2022 What does your App\Models\Admin class look like? 1 Quote Link to comment Share on other sites More sharing options...
ali_254 Posted August 9, 2022 Author Share Posted August 9, 2022 (edited) 53 minutes ago, maxxd said: <?php namespace App\Models; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; class Admin extends Authenticatable { use HasApiTokens; use HasFactory; use Notifiable; /** * The attributes that are mass assignable. * * @var array */ // protected $fillable = [ // 'name', // 'email', // 'password', // ]; protected $guarded = []; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', 'two_factor_recovery_codes', 'two_factor_secret', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; /** * The accessors to append to the model's array form. * * @var array */ protected $appends = [ 'profile_photo_url', ]; } When I create a guard, which middleware should I use? Have I chosen the right middleware? After logging in, Returns the details of the admin table: if(Auth::guard('admin')->attempt($credentials)) { dd (Auth::guard('admin')->user()); //return true But in the controller on which I applied middleware it returns null Edited August 9, 2022 by ali_254 Quote Link to comment Share on other sites More sharing options...
ali_254 Posted August 24, 2022 Author Share Posted August 24, 2022 https://laracasts.com/discuss/channels/laravel/authguardadmin-user-returns-null?page=1&replyId=819267 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.