In api.php
routes file below, there are public routes and private routes:
Route::group([\'namespace\' => \'API\'], function() {
// Publ
Pass the api
guard as a parameter to fetch the authorized user without the middleware protecting the request.
$request->user('api');
// Or
auth('api')->user();
You are referencing Request
from a root namespace: \Request
. Instead, you should reference the Illuminate\Http\Request
class.
You should remove the \
from your parameter and add the following line to your imports.
use Illuminate\Http\Request;
Alternatively, you could also reference the request class directly in your method:
class TestController extends Controller {
public function testauth1(Illuminate\Http\Request $request) {
return $request->user();
}
public function testauth2() {
return auth()->user(); // returns user
}
}
The auth() helper method or Auth Facade is globally available. It doesn't depend on the request that you are trying to access. The same goes for the request()
and Request::
helpers I believe. In the case you are giving, you are referencing a wrong Request instance, hence giving a unexpected result.