在個人資料頁面中,用戶可以選擇選擇頭像圖片,在header.blade.php我想檢查用戶是否選擇了頭像,如果是,則在用戶名旁邊顯示頭像圖片。現在profile.blade.php我可以用這行代碼顯示所選的頭像
<img style="border-radius: 50%;" src="{{url('/')}}/Uploads/avatars/{{$myAvatar->image}}" height="60px;" width="60px;" alt="" id="avatar-preload" name="avatar-preload">
在組態檔控制器中,我有傳遞$myAvatar給的索引函式profile.blade.php
public function index()
{
$user= User::where('id', Auth::user()->id)->first();
$myAvatar = Avatar::find($user->avatar_id);
return view('admin.profile.index')
->with('avatars', Avatar::all())
->with('myAvatar',$myAvatar);
}
我想應用相同的邏輯將頭像圖片帶到,header.blade.php但我沒有控制器通過它向標題刀片發送資料,我該怎么做?
每個用戶現在都有一個avatar_id,它指的是頭像表中所選頭像的 id
uj5u.com熱心網友回復:
在您的User模型中,您可以定義與 Avatar 的關系
public function avatar()
{
return $this->hasOne(Avatar::class, 'id','avatar_id');
}
然后在您的刀片中,您可以Auth::user()->avatar隨心所欲地使用。
uj5u.com熱心網友回復:
創建模型關系
public function getAvtar()
{
return Avatar::where('your filed name',Auth::user()->id);
}
然后像這樣打電話
Auth::user()->getAvtar()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/370935.html
標籤:拉拉维尔
