F
Filament17mo ago
N1XN

RelationManager attempts to lazy load relations

I have created an Attribute to display a range, which combines min and max BelongsTo Relations, but I get the enabled strictMode error thrown. Attempted to lazy load [minDose] on model [App\Models\PatientMedication] but lazy loading is disabled. Is there a way how I can manually load the needed relations via TextColumn API?
// MedicationDoseRelationManager
// ...
TextColumn::make('medicationDoseRange')
->label('Dose'),
// ...
// MedicationDoseRelationManager
// ...
TextColumn::make('medicationDoseRange')
->label('Dose'),
// ...
class PatientMedication extends Model
{
public function minDose(): BelongsTo
{
return $this->belongsTo(Dose::class, 'min_dose_id');
}

public function maxDose(): BelongsTo
{
return $this->belongsTo(Dose::class, 'max_dose_id');
}

protected function medicationDoseRange(): Attribute
{
return Attribute::make(
get: function () {
$consolidatedValue = $this->minDose->name === $this->maxDose->name; // Error get trown

return $consolidatedValue
? $this->minDose->name
: $this->minDose->name.' - '.$this->maxDose->name;
}
);
}
}
class PatientMedication extends Model
{
public function minDose(): BelongsTo
{
return $this->belongsTo(Dose::class, 'min_dose_id');
}

public function maxDose(): BelongsTo
{
return $this->belongsTo(Dose::class, 'max_dose_id');
}

protected function medicationDoseRange(): Attribute
{
return Attribute::make(
get: function () {
$consolidatedValue = $this->minDose->name === $this->maxDose->name; // Error get trown

return $consolidatedValue
? $this->minDose->name
: $this->minDose->name.' - '.$this->maxDose->name;
}
);
}
}
2 Replies
AlexAnder
AlexAnder17mo ago
Extend eloquent query, add with(['minDose', 'maxDose']);
N1XN
N1XNOP17mo ago
I have done this in DoctorResource @AlexAnder but still getting lazy loading error.
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->with([
'patients.latestPatientMediaction.minDose',
'patients.latestPatientMediaction.maxDose']);
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->with([
'patients.latestPatientMediaction.minDose',
'patients.latestPatientMediaction.maxDose']);
}
Contrary this call works without ever setting the with in the query.
TextColumn::make('latestPatientMediaction.minDose.name')
->label('min. dose')
->sortable(),
TextColumn::make('latestPatientMediaction.maxDose.name')
->label('max. dose')
->sortable(),
TextColumn::make('latestPatientMediaction.minDose.name')
->label('min. dose')
->sortable(),
TextColumn::make('latestPatientMediaction.maxDose.name')
->label('max. dose')
->sortable(),

Did you find this page helpful?