Say I have many clubs that all need to pay a yearly subscription, so will have several subscription records.
I've set up my relationship:
In Club.php
public function subscriptionRecords() {
return $this->hasMany('App\ClubSubscriptionRecord');
}
But now I want to set up something where I can easily check if each club has paid their subscription in the current year or not, and would like easy access to it like just do $club->getSubscriptionPaid and get a boolean back.
which I've tried to do as an Accessor in Club.php like this:
public function getSubscriptionPaid() {
$lastPaymentDateYear = $this->subscriptionRecords()->orderBy('payment_date')->pluck('payment_date')->last()->year;
$currentYear = Carbon::now()->year;
if ($lastPaymentDateYear = $currentYear) {
return true;
} else {
return false;
}
}
This get's me the error:
LogicException with message 'Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
...so I guess I'm not supposed to do it as an Accessor, but I don't what it's called what I want to do so very difficult to search for how/where to do it.
Any help would be appreciated.