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]); }
public function postFavoriteToggle($id) { $id = (int) $id; if (Project::where('id', $id)->exists()) { $userId = auth()->id(); $action = strtolower(request()->input('action')); $exist = ProjectFavorite::where('user_id', $userId)->where('project_id', $id)->exists(); if ($action == 'add' && !$exist) { $object = new ProjectFavorite(); $object->user_id = $userId; $object->project_id = $id; $object->save(); } elseif ($action == 'del' && $exist) { ProjectFavorite::where('user_id', $userId)->where('project_id', $id)->delete(); } return response()->json(['status' => true, 'message' => '']); } return response()->json(['status' => false, 'message' => '你要收藏的项目不存在']); }