build( SiteProject::selectRaw('id, name, office_address, phone_number'), (new PaginateBuilderAttr)->setSearchKeyword($search), new PaginateSchemaSiteProject ); } public function paginate(PaginateBuilderAttr $paginateBuilderAttr): PaginateCollectionAttr { return (new PaginateCollectionAttrBuilder) ->build( SiteProject::query(), $paginateBuilderAttr, new PaginateSchemaSiteProject ); } public function fetch($id) { $res = SiteProject::findOrFail($id); return $res; } public function create(SiteProjectAttr $siteProjectAttr) { $res = SiteProject::create([ 'name' => $siteProjectAttr->getName(), 'office_address' => $siteProjectAttr->getOfficeAddress(), 'phone_number' => $siteProjectAttr->getPhoneNumber() ]); return $res; } public function update(SiteProjectAttr $siteProjectAttr, $id) { $res = $this->fetch($id); $res->update([ 'name' => $siteProjectAttr->getName(), 'office_address' => $siteProjectAttr->getOfficeAddress(), 'phone_number' => $siteProjectAttr->getPhoneNumber() ]); return $res; } public function delete($id) { $res = $this->fetch($id); $res->delete(); return $res; } public function all() { return SiteProject::all(); } public function indexByServiceCategory($id) { return SiteProject::withoutGlobalScope('syncActiveServiceCategory')->where('service_category_id', $id)->get(); } public function select2ByCityBranch(?string $search, $id): PaginateCollectionAttr { $auth = Auth::guard('jwt_mobile')->user() ?? Auth::user(); $siteProjectId = []; foreach ($auth->userRole->role->siteProjects ?? [] as $siteProject) { $siteProjectId[] = $siteProject->id; } if (getAuthRoleName() === 'super admin') { $siteProject = SiteProject::selectRaw('id, name as text')->withoutGlobalScope('syncActiveServiceCategory')->withoutGlobalScope('syncActiveCityBranch')->where('city_branch_id', $id); } else { $siteProject = SiteProject::selectRaw('id, name as text')->withoutGlobalScope('syncActiveServiceCategory')->withoutGlobalScope('syncActiveCityBranch')->where('city_branch_id', $id)->whereIn('id', $siteProjectId); } return (new PaginateCollectionAttrBuilder) ->build( $siteProject, (new PaginateBuilderAttr)->setSearchKeyword($search), new PaginateSchemaSiteProject ); } public function select2ByCityBranchArray(?string $search, $id): PaginateCollectionAttr { $array = explode(",", $id); return (new PaginateCollectionAttrBuilder) ->build( SiteProject::selectRaw('id, name as text, name as label')->withoutGlobalScope('syncActiveServiceCategory')->withoutGlobalScope('syncActiveCityBranch')->whereIn('city_branch_id', $array), (new PaginateBuilderAttr)->setSearchKeyword($search), new PaginateSchemaSiteProject ); } public function select2WhereNotActive(?string $search, $id): PaginateCollectionAttr { $array = explode(",", $id); return (new PaginateCollectionAttrBuilder) ->build( SiteProject::selectRaw('id, name as text, name as label')->withoutGlobalScope('syncActiveCityBranch')->whereIn('city_branch_id', $array)->where('id', '!=', getActiveSiteProjectId()), (new PaginateBuilderAttr)->setSearchKeyword($search), new PaginateSchemaSiteProject ); } public function paginateByServiceCategory(PaginateBuilderAttr $paginateBuilderAttr): PaginateCollectionAttr { return (new PaginateCollectionAttrBuilder) ->build( SiteProject::with('province', 'city', 'subdistrict', 'cityBranch', 'cityCenter', 'subdistrictCenter')->withoutGlobalScope('syncActiveCityBranch')->withoutGlobalScope('syncActiveSiteProject'), $paginateBuilderAttr, new PaginateSchemaSiteProject ); } public function fetchByServiceCategory($id) { $res = SiteProject::withoutGlobalScope('syncActiveCityBranch')->with('province', 'city', 'subdistrict', 'cityBranch', 'cityCenter', 'subdistrictCenter')->find($id); return $res; } public function listByCityBranchArray($cityBranches) { $array = explode(",", $cityBranches); return SiteProject::select('id' ,'name as label', 'city_branch_id')->whereIn('city_branch_id', $array)->withoutGlobalScope('syncActiveServiceCategory')->withoutGlobalScope('syncActiveCityBranch')->get(); } }