*/ public $guarded = []; /** @var array */ public $casts = [ 'from' => 'date', 'to' => 'date', 'promised_at' => 'date', ]; /** * @return BelongsTo */ public function activity(): BelongsTo { return $this->belongsTo(Activity::class); } /** * @return BelongsTo */ public function group(): BelongsTo { return $this->belongsTo(Group::class); } /** * @return BelongsTo */ public function subactivity(): BelongsTo { return $this->belongsTo(Subactivity::class); } /** * @return BelongsTo */ public function member(): BelongsTo { return $this->belongsTo(Member::class); } public function isActive(): bool { return $this->from->isBefore(now()) && (null === $this->to || $this->to->isAfter(now())); } /** * @param Builder $query * * @return Builder */ public function scopeActive(Builder $query): Builder { return $query->where('from', '<=', now()) ->where(fn ($query) => $query->whereNull('to')->orWhere('to', '>=', now())); } /** * @param Builder $query * * @return Builder */ public function scopeIsAgeGroup(Builder $query): Builder { return $query->whereHas('subactivity', fn ($builder) => $builder->where('is_age_group', true)); } /** * @param Builder $query * * @return Builder */ public function scopeIsMember(Builder $query): Builder { return $query->whereHas('activity', fn ($builder) => $builder->where('is_member', true)); } /** * @param Builder $query * * @return Builder */ public function scopeIsLeader(Builder $query): Builder { return $query->whereHas('activity', fn ($builder) => $builder->where('has_efz', true)); } /** * @param Builder $query * * @return Builder */ public function scopeIsTrying(Builder $query): Builder { return $query->active()->whereHas('activity', fn ($builder) => $builder->where('is_try', true)); } public static function booted(): void { static::saved(function ($membership) { $membership->member->touch(); }); static::deleted(function ($membership) { $membership->member->touch(); }); } }