Auth
Inject an auth guard instance
Description: Injects an auth guard instance into the constructor or method.
Namespace: Illuminate\Container\Attributes\Auth
Added in: Laravel 11.20
Usage
use Illuminate\Support\Facades\Auth;
use Illuminate\Contracts\Auth\Guard;
// Before (service provider):
$this->app->when(UserService::class)
->needs(Guard::class)
->give(fn () => Auth::guard('web'));
use Illuminate\Container\Attributes\Auth;
use Illuminate\Contracts\Auth\Guard;
// After:
class UserService
{
public function __construct(
#[Auth('web')] private readonly Guard $guard
) {}
public function currentUser()
{
return $this->guard->user();
}
}