'integer', 'send_newspaper' => 'boolean', 'gender_id' => 'integer', 'way_id' => 'integer', 'country_id' => 'integer', 'region_id' => 'integer', 'confession_id' => 'integer', 'nami_id' => 'integer', 'is_confirmed' => 'boolean', ]; public function scopeSearch($q, $text) { return $q->where('firstname', 'LIKE', '%'.$text.'%') ->orWhere('lastname', 'LIKE', '%'.$text.'%') ->orWhere('address', 'LIKE', '%'.$text.'%') ->orWhere('zip', 'LIKE', '%'.$text.'%') ->orWhere('location', 'LIKE', '%'.$text.'%'); } //----------------------------------- Getters ----------------------------------- public function getFullnameAttribute() { return $this->firstname.' '.$this->lastname; } public function getHasNamiAttribute() { return $this->nami_id !== null; } public function getNamiMemberships($api) { return $api->group($this->group->nami_id)->member($this->nami_id)->memberships()->toArray(); } public function getNamiFeeId() { if (!$this->subscription) { return null; } return $this->subscription->fee->nami_id; } //---------------------------------- Relations ---------------------------------- public function country() { return $this->belongsTo(\App\Country::class); } public function gender() { return $this->belongsTo(\App\Gender::class); } public function region() { return $this->belongsTo(\App\Region::class); } public function confession() { return $this->belongsTo(\App\Confession::class); } public function payments() { return $this->hasMany(Payment::class)->orderBy('nr'); } public function way() { return $this->belongsTo(App\Way::class); } public function nationality() { return $this->belongsTo(Nationality::class); } public function memberships() { return $this->hasMany(Membership::class); } public function subscription() { return $this->belongsTo(Subscription::class); } public function billKind() { return $this->belongsTo(BillKind::class); } public function group() { return $this->belongsTo(Group::class); } public function firstActivity() { return $this->belongsTo(Activity::class, 'first_activity_id'); } public function firstSubActivity() { return $this->belongsTo(Subactivity::class, 'first_subactivity_id'); } public static function booted() { static::deleting(function($model) { $model->payments->each->delete(); }); } // ---------------------------------- Scopes ----------------------------------- public function scopeWithIsConfirmed($q) { $q->selectSub('DATEDIFF(NOW(), IFNULL(confirmed_at, DATE_SUB(NOW(), INTERVAL 3 YEAR))) < 712', 'is_confirmed'); } public function scopeWithSubscriptionName($q) { return $q->addSelect([ 'subscription_name' => Subscription::select('name')->whereColumn('subscriptions.id', 'members.subscription_id')->limit(1) ]); } public function scopeWithPendingPayment($q) { return $q->addSelect([ 'pending_payment' => Payment::selectRaw('SUM(subscriptions.amount)') ->whereColumn('payments.member_id', 'members.id') ->whereNeedsPayment() ->join('subscriptions', 'subscriptions.id', 'payments.subscription_id') ]); } public function scopePayable($q) { $q->where('bill_kind_id', '!=', null)->where('subscription_id', '!=', null); } public function scopeWhereNoPayment($q, $year) { return $q->whereDoesntHave('payments', function($q) use ($year) { return $q->where('nr', '=', $year); }); } }