Laravel Eloquent: Create custom attribute from subquery
Modal: Subscription.php class Subscription extends Model { use HasFactory; public function payments(): HasMany { return $this->hasMany(SubscriptionPayment::class, 'sub_id'); } public function totalDurationDays(): Attribute { return Attribute::make( get: fn() => $this->payments()->sum('duration_days') )->shouldCache(); } } Controller: SubscriptionController.php $subs = SharedServer::from('subscriptions AS ss') ->select(…