[英]Unable to pass $user variable using Auth::user() in Laravel Profile controller
I just started learning Laravel a week ago, and I've run into an issue with middleware and using Auth::user().我一周前才开始学习 Laravel,但遇到了中间件和使用 Auth::user() 的问题。 For Laravel >5.3, you can no longer pass session variables within the controller.
对于 Laravel > 5.3,您不能再在控制器内传递会话变量。 I want to be able to pass the $user model back into the blade file (entered in as {{$user->avatar}} within an URL string), but I don't know how to do it.
我希望能够将 $user 模型传递回刀片文件(在 URL 字符串中作为 {{$user->avatar}} 输入),但我不知道该怎么做。
I spent an entire day (based on suggestions from https://laravel.com/docs/5.3/upgrade#5.3-session-in-constructors )), and I consistently get null values, with the error (Undefined variable: user).我花了一整天的时间(根据https://laravel.com/docs/5.3/upgrade#5.3-session-in-constructors 的建议),我始终得到空值,并出现错误(未定义变量:用户) . I'm really at a loss of how to proceed, and will be grateful if anyone could help me.
我真的不知道如何继续,如果有人能帮助我,我将不胜感激。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use App\Http\Requests;
use App\User;
use Auth;
use Image;
class ProfileController extends Controller
{
public function profile($id)
{
$user = User::where('id', $id)->first();
return view('user.profile',compact('user'));
}
public function update_avatar(Request $request)
{
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 300)->save( public_path('/uploads/avatars/' . $filename));
$user = Auth::user();
$user->avatar = $filename;
$user->save();
dd($user);
}
return view('user.profile',compact('user'));
}
}
You don't need to pass an authenticated user from the controller.您不需要从控制器传递经过身份验证的用户。
In your Blade template, just directly use this, for example:在你的 Blade 模板中,直接使用这个,例如:
{{ Auth::user()->id }}
This assumes User is logged in.这假设用户已登录。
As your dd(Auth::user())
inside the profile()
method returned your expected data, try this:由于
profile()
方法中的dd(Auth::user())
返回了您的预期数据,请尝试以下操作:
public function profile(){
$user = Auth::user();
return view('profile', compact('user'));
}
You should then be able to do {{ $user->id }}
in your blade file.然后您应该能够在您的刀片文件中执行
{{ $user->id }}
。 I know the above code is similar to what you have but it's worth a shot!我知道上面的代码与您拥有的代码相似,但值得一试!
If you create your own admin auth guard.如果您创建自己的管理员身份验证保护。 As your dd(Auth::guard('admin')->user()) inside the your profile() method returned your expected data for login admin user, try this:
由于您的 profile() 方法中的 dd(Auth::guard('admin')->user()) 返回了登录管理员用户的预期数据,请尝试以下操作:
public function profile(){
$user = Auth::guard('admin')->user();
return view('profile', compact('user'));
}
You should then be able to do {{ $user->id }} in your blade file.然后您应该能够在您的刀片文件中执行 {{ $user->id }} 。 I know the above code is similar to what you have but it's worth a short.
我知道上面的代码与您所拥有的相似,但值得一试。
if you create your own auth guard use tha auth data then use user() method with your auth guard name.如果您创建自己的 auth 守卫使用 tha auth 数据,然后使用 user() 方法和您的 auth 守卫名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.