简体   繁体   中英

Auth::id() returns null laravel

i have a function where

public function index(){
   $users = User::doesntHave('roles')->latest()->paginate()->except(Auth::id());
   return UsersResource::collection($users);
}

when i dd the Auth::id() it returns null even if I declared the auth facade on my controller

use Illuminate\Support\Facades\Auth;

this is my route which is stored inside api.php

Route::resource('users','Users\AdminUsersController')->except([
    'create', 'edit'
]);

Add your auth protected routes inside auth:api middleware

Route::post('login','LoginController@login');

Route::middleware(['auth:api'])->group(function () {

    Route::resource('users','Users\AdminUsersController')->except([
    'create', 'edit'
    ]);

    //other authenticated Routes goes inside this block

}); 

For Api authentication i suggest you to look https://laravel.com/docs/5.6/passport

Are you're signed in. It doesn't sound like it.

Don't use Illuminate\\Support\\Facades\\Auth; instead it is just use Auth ;.

You can do the following:

auth()->check(); // this checks to see if you're logged in

$userId = auth()->check() ? auth()->id() : null;

auth()->id(); // this is the same as Auth::id();

auth()->user();

$ users = User :: doesntHave('roles') - > latest() - > paginate() - > except(Auth :: user() - > id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM