WebComposer.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Http\ViewComposers;
  3. use Illuminate\Support\Facades\Auth;
  4. use Illuminate\View\View;
  5. use App\Models\User;
  6. class WebComposer
  7. {
  8. // 当前登录用户
  9. protected $currentUser;
  10. /**
  11. * 创建一个新的 profile composer
  12. *
  13. * @return void
  14. */
  15. public function __construct(User $user)
  16. {
  17. // 当前登录用户
  18. $this->currentUser = null;
  19. if (Auth::check()) {
  20. // 当前登录用户 id
  21. $authId = Auth::user()->id;
  22. // 获取当前登录用户 信息
  23. $this->currentUser = $user
  24. ->select('users.*', 'A.path as avatar_path', 'B.id as user_infos_id')
  25. ->addSelect('B.gender', 'B.github_name', 'B.real_name', 'B.city', 'B.company', 'B.jobtitle', 'B.personal_website', 'B.wechat_qrcode', 'B.payment_qrcode', 'B.introduction', 'B.signature', 'B.avatar', 'B.image_id', 'B.user_id')
  26. ->where('users.id', '=', $authId)
  27. ->leftJoin('images as A', function ($join){
  28. $join->on('A.user_id', '=', 'users.id')
  29. ->where('A.image_type', '=', 'avatar');
  30. })
  31. ->leftJoin('user_infos as B', 'B.user_id', '=', 'users.id')
  32. ->first();
  33. }
  34. }
  35. /**
  36. * 将数据绑定到视图。
  37. *
  38. * @param View $view
  39. * @return void
  40. */
  41. public function compose(View $view)
  42. {
  43. $view->with(['common'=> [
  44. 'auth'=> $this->currentUser,
  45. ]]);
  46. }
  47. }