F
Filament17mo ago
N1XN

Sum latest HasThroughMany

How can I sum the latestPatientProtcols.hours within a DoctorResource ? Doctor -> hasMany Patient -> hasMany -> PatientProtocol
TextColumn::make('latest_patient_protocols_sum_hours')
->sum('latestPatientProtocols', 'cubic_value'),
TextColumn::make('latest_patient_protocols_sum_hours')
->sum('latestPatientProtocols', 'cubic_value'),
<?php

class Doctor extends BaseModel
{
public function patients(): HasMany
{
return $this->hasMany(Patient::class);
}

public function latestPatientProtocols(): Collection
{
return $this->patients->map(fn (Patient $patients) => $patients->latestPatientProtocols)->filter();
}
<?php

class Doctor extends BaseModel
{
public function patients(): HasMany
{
return $this->hasMany(Patient::class);
}

public function latestPatientProtocols(): Collection
{
return $this->patients->map(fn (Patient $patients) => $patients->latestPatientProtocols)->filter();
}
<?php

namespace App\Models;

class Patient extends BaseModel
{
public function doctor(): BelongsTo
{
return $this->belongsTo(Doctor::class);
}

public function latestPatientProtocol(): HasOne
{
return $this->hasOne(PatientProtocol::class)
->latestOfMany('created_at');
}

public function patientProtocols(): HasMany
{
return $this->hasMany(PatientProtocol::class);
}
}
<?php

namespace App\Models;

class Patient extends BaseModel
{
public function doctor(): BelongsTo
{
return $this->belongsTo(Doctor::class);
}

public function latestPatientProtocol(): HasOne
{
return $this->hasOne(PatientProtocol::class)
->latestOfMany('created_at');
}

public function patientProtocols(): HasMany
{
return $this->hasMany(PatientProtocol::class);
}
}
<?php

namespace App\Models;

class PatientProtocol extends Model
{
public function patient(): BelongsTo
{
return $this->belongsTo(Patient::class);
}
}
<?php

namespace App\Models;

class PatientProtocol extends Model
{
public function patient(): BelongsTo
{
return $this->belongsTo(Patient::class);
}
}
Solution:
Figured it out but, filament now completely ignores the subquery and returns the sum of all protocols.... Seems like a bug ```php public function latestPatientProtocols(): HasManyThrough...
Jump to solution
1 Reply
Solution
N1XN
N1XN17mo ago
Figured it out but, filament now completely ignores the subquery and returns the sum of all protocols.... Seems like a bug
public function latestPatientProtocols(): HasManyThrough
{
$subquery = PatientProtocol::select('patient_id')
->selectSub('MAX(id)', 'max_id')
->groupBy('patient_id');

return $this->hasManyThrough(
PatientProtocol::class,
Patient::class
)->joinSub($subquery, 'latest_protocols', function ($join) {
$join->on('patient_protocols.id', '=', 'latest_protocols.max_id');
});
}
public function latestPatientProtocols(): HasManyThrough
{
$subquery = PatientProtocol::select('patient_id')
->selectSub('MAX(id)', 'max_id')
->groupBy('patient_id');

return $this->hasManyThrough(
PatientProtocol::class,
Patient::class
)->joinSub($subquery, 'latest_protocols', function ($join) {
$join->on('patient_protocols.id', '=', 'latest_protocols.max_id');
});
}
TextColumn::make('latest_patient_protocols_sum_metric_value')
->sum('latestPatientProtocols', 'metric_value')
->label('Patient Metric')
->suffix(' units'),
TextColumn::make('latest_patient_protocols_sum_metric_value')
->sum('latestPatientProtocols', 'metric_value')
->label('Patient Metric')
->suffix(' units'),

Did you find this page helpful?