ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
当用户通过认证后,有几种方式取得用户实例。 首先, 你可以从 Auth facade 取得用户: ~~~ <?php namespace App\Http\Controllers; use Illuminate\Routing\Controller; class ProfileController extends Controller { /** * Update the user's profile. * * @return Response */ public function updateProfile() { if (Auth::user()) { // Auth::user() returns an instance of the authenticated user... } } } ~~~ 第二种,你可以使用 Illuminate\Http\Request 实例取得认证过的用户: ~~~ <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Routing\Controller; class ProfileController extends Controller { /** * Update the user's profile. * * @return Response */ public function updateProfile(Request $request) { if ($request->user()) { // $request->user() returns an instance of the authenticated user... } } } ~~~ 第三,你可以使用 Illuminate\Contracts\Auth\Authenticatable contract 类型提示。这个类型提示可以用在控制器的构造方法,控制器的其他方法,或是其他可以通过服务容器 解析的类的构造方法: ~~~ <?php namespace App\Http\Controllers; use Illuminate\Routing\Controller; use Illuminate\Contracts\Auth\Authenticatable; class ProfileController extends Controller { /** * Update the user's profile. * * @return Response */ public function updateProfile(Authenticatable $user) { // $user is an instance of the authenticated user... } } ~~~