'clearSearchGroup', 'newGroupCreated' => 'newGroupCreated', 'searchGroup' => 'searchGroup', ]; public function mount() { $this->getGroupsCount(); } /** * @return Application|Factory|View */ public function render() { $this->searchGroup(); return view('chat::livewire.group-search'); } public function clearSearchGroup() { $this->searchTerm = ''; $this->searchGroup(); } /** * search users and apply filters */ public function searchGroup() { $groups = $this->getGroupQuery() ->when($this->searchTerm, function ($query) { return $query->whereRaw('name LIKE ?', ['%'.strtolower($this->searchTerm).'%']); }) ->select(['id', 'photo', 'name']) ->orderBy('name', 'asc') ->limit(20) ->get(); $this->groups = $groups; } public function newGroupCreated() { if ($this->groupsCount == 0) { $this->getGroupsCount(); } } public function getGroupsCount() { $this->groupsCount = $this->getGroupQuery()->count(); } /** * @return Builder */ public function getGroupQuery() { return Group::with([ 'users' => function (BelongsToMany $query) { $query->toBase()->select('id'); }, 'usersWithTrashed', ])->whereHas('users', function (Builder $query) { $query->where('user_id', getLoggedInUserId()); }); } }