public function index($userId = null) { $auth = auth(); if (!$userId) { $userId = $auth->id(); $isAuthUser = true; } else { $isAuthUser = $userId == $auth->id(); } if ($isAuthUser) { $user = $auth->user(); } else { $user = User::findOrFail($userId); } $count['create'] = Project::where('user_id', $userId)->count(); $count['join'] = Project::join('project_members', function ($join) use($userId) { $join->on('projects.id', '=', 'project_members.project_id')->where('project_members.user_id', '=', $userId); })->where('projects.user_id', '<>', $userId)->count(); $count['comment'] = 0; $count['collect'] = ProjectFavorite::where('user_id', $userId)->count(); $showList = 'create'; foreach ($count as $key => $value) { if ($value > 0) { $showList = $key; break; } } return view('user.home.index', ['userData' => $user, 'isAuthUser' => $isAuthUser, 'projectCount' => $count, 'showList' => $showList]); }
protected function getUserProjectCount($userId) { $count = []; $count['create'] = Project::where('user_id', $userId)->count(); $count['join'] = Project::join('project_members', function ($join) use($userId) { $join->on('projects.id', '=', 'project_members.project_id')->where('project_members.user_id', '=', $userId); })->where('projects.user_id', '<>', $userId)->count(); $count['comment'] = 0; $count['collect'] = 0; return $count; }
protected function getCollect($startId, $userId) { $this->startIdKey = 'favorite_id'; $startId = intval($startId); $builder = Project::join('project_favorites', function ($join) use($startId, $userId) { $join->on('projects.id', '=', 'project_favorites.project_id')->where('project_favorites.user_id', '=', $userId); if ($startId > 0) { $join->where('id', '<', $startId); } })->where('projects.user_id', '<>', $userId); return $builder->orderBy('project_favorites.id', 'desc')->take($this->listTake)->get(['projects.*', 'project_favorites.id as favorite_id']); }