hasMany(Claim::class, 'member_id', 'id'); } public function employeds() { return $this->hasMany(CorporateEmployee::class, 'member_id'); } public function corporates() { return $this ->belongsToMany(Corporate::class, 'corporate_employees', 'corporate_id', 'member_id') ->withPivot([ 'branch_code', 'divison_id', 'nik', 'status', 'start', 'end' ]); } public function currentCorporate() { // return $this->belongsToMany(Corporate::class, 'corporate_employees', 'corporate_id', 'member_id') // // ->withPivot([ // // 'branch_code', // // 'divison_id', // // 'nik', // // 'status', // // 'start', // // 'end' // // ]) // ->where('start', '<', now()) // ->where('end', '>', now()); return $this->hasOneThrough(Corporate::class, CorporateEmployee::class, 'member_id', 'id', 'id', 'corporate_id'); // ->where('corporate_policies.start', '<', now()) // ->where('corporate_policies.end', '>', now()) // ->where('member_policies.start', '<', now()) // ->where('member_policies.end', '>', now()); } public function memberPlans() { return $this->hasMany(MemberPlan::class, 'member_id'); } public function plans() { return $this->hasManyThrough(Plan::class, MemberPlan::class, 'member_id', 'id', 'id', 'plan_id'); } public function currentPlan() { return $this->hasOneThrough(Plan::class, MemberPlan::class, 'member_id', 'id', 'id', 'plan_id')->latest(); } public function policies() { return $this->hasMany(MemberPolicy::class, 'member_id', 'member_id'); } public function currentPolicy() { return $this->hasOneThrough(CorporatePolicy::class, MemberPolicy::class, 'member_id', 'code', 'member_id', 'policy_id') ->where('corporate_policies.start', '<', now()) ->where('corporate_policies.end', '>', now()) ->where('member_policies.start', '<', now()) ->where('member_policies.end', '>', now()); // return $this->hasOne(MemberPolicy::class, 'member_id', 'member_id')->where('status', 'active')->latestOfMany(); } public function getAgeAttribute() { if ($this->birth_date) { return Carbon::parse($this->birth_date)->diffInYears(now()); } else { return null; } } public function getFullNameAttribute() { $arr = []; if (!empty($this->name_prefix)) { $arr[] = $this->name_prefix; } $arr[] = $this->name; if (!empty($this->name_suffix)) { $arr[] = $this->name_suffix; } return implode(' ', $arr); } public function getGenderCodeAttribute() { return $this->gender ? ($this->gender == 'female' ? 'F' : 'M') : $this->gender; } public function scopeFilter($query, array $filters) { $query->when($filters['search'] ?? false, function ($query, $search) { return $query ->where('member_id', 'like', "%" . $search . "%") ->orWhere('payor_id', 'like', "%" . $search . "%") ->orWhere('name', 'like', "%" . $search . "%") ; // ->orWhereHas('corporatePlan', function ($query) use ($search) { // $query->where('code', 'like', "%" . $search . "%"); // }); }); } }