/** * @param string $attribute * * @return array * * @throws ModelNotFoundException */ public function getUseridArray($attribute) { $usernames = $this->getUsernameArray($attribute); $userids = array(); foreach ($usernames as $username) { $user = User::where('name', $username)->first(); if (!$user) { throw new ModelNotFoundException(); } $userids[] = $user->id; } return $userids; }
/** * @param User $user * @param ProfileField $profileField * * @return bool */ public function hasForProfileField(User $user, ProfileField $profileField) { return $this->userProfileField->where('user_id', $user->getId())->where('profile_field_id', $profileField->id)->count() > 0; }
private function recountUsers() { $this->info('Recounting user counters...'); $users = User::all(); foreach ($users as $user) { $user->num_posts = Post::where('user_id', '=', $user->id)->count(); $user->num_topics = Topic::where('user_id', '=', $user->id)->count(); $user->save(); } $this->info('Done' . PHP_EOL); }
/** * @param Model $content * * @return mixed */ public function removeLikesForContent(Model $content) { $baseQuery = $this->likesModel->where('content_type', '=', get_class($content))->where('content_id', '=', $content->id); $likes = $baseQuery->get(); foreach ($likes as $like) { User::find($like->user_id)->decrement('num_likes_made'); } return $baseQuery->delete(); }
/** * Find a single user by its username. * * @param string $username The username of the user. Eg: 'admin'. * * @return mixed */ public function findByUsername($username = '') { return $this->userModel->whereNname($username)->first(); }
/** * Create a new topic * * @param array $details Details about the topic. * * @return mixed */ public function create(array $details = []) { $details = array_merge(['title' => '', 'forum_id' => 0, 'user_id' => $this->guard->user()->id, 'username' => null, 'first_post_id' => 0, 'last_post_id' => 0, 'views' => 0, 'num_posts' => 0, 'content' => ''], $details); $details['slug'] = $this->createSlugForTitle($details['title']); if ($details['user_id'] > 0) { $details['username'] = User::find($details['user_id'])->name; // TODO: Use User Repository! } else { $details['user_id'] = null; if ($details['username'] == trans('general.guest')) { $details['username'] = null; } } $topic = null; $this->dbManager->transaction(function () use($details, &$topic) { $topic = $this->topicModel->create(['title' => $details['title'], 'slug' => $details['slug'], 'forum_id' => $details['forum_id'], 'user_id' => $details['user_id'], 'username' => $details['username']]); $firstPost = $this->postRepository->addPostToTopic($topic, ['content' => $details['content'], 'username' => $details['username']]); $topic->update(['first_post_id' => $firstPost->id, 'last_post_id' => $firstPost->id, 'num_posts' => 1]); }); $topic->forum->increment('num_topics'); if ($topic->user_id > 0) { $topic->author->increment('num_topics'); } return $topic; }
/** * Add a post to a topic. * * @param Topic $topic The topic to add a post to. * @param array $postDetails The details of the post to add. * * @return mixed */ public function addPostToTopic(Topic $topic, array $postDetails) { $postDetails = array_merge(['user_id' => $this->guard->user()->id, 'username' => null, 'content' => '', 'content_parsed' => ''], $postDetails); $postDetails['content_parsed'] = $this->formatter->parse($postDetails['content'], [MessageFormatter::ME_USERNAME => $this->guard->user()->name]); // TODO: Parser options... if ($postDetails['user_id'] > 0) { $postDetails['username'] = User::find($postDetails['user_id'])->name; } else { $postDetails['user_id'] = null; if ($postDetails['username'] == trans('general.guest')) { $postDetails['username'] = null; } } $post = $topic->posts()->create($postDetails); if ($post !== false) { $topic->increment('num_posts'); $topic->update(['last_post_id' => $post['id']]); $topic->forum->increment('num_posts'); $topic->forum->update(['last_post_id' => $post->id, 'last_post_user_id' => $postDetails['user_id']]); } if ($post->user_id > 0) { $post->author->increment('num_posts'); } return $post; }
/** * Create a new user instance after a valid registration. * * @param array $data * * @return User */ protected function create(array $data) { $user = User::create(['name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password'])]); $user->roles()->attach(Role::where('role_slug', '=', 'user')->pluck('id'), ['is_display' => true]); return $user; }