Relation manager pulls wrong data

I have built a demo EHR system around medical records- I may not have architectured this correctly but i want to have appointments and prescriptions link so when a patient gets prescribed something its linked to an appointment, doctor, patient. i created PrescriptionsRelationManager which when i added to my ViewAppointment page it loads information about appointments rather than prescriptions?
10 Replies
toeknee
toeknee2w ago
It looks good. You create method also says prescription so where you are getting new appintment from I am unsure. Check what your relationship 'prescriptions' is.
chris.mccabe
chris.mccabeOP2w ago
you are correct i had the wrong model class in my relation on the Appointment Model @toeknee another question, how do i overwride the relation manager query to ignore the appointment_id? yes prescriptions have an appointment_id in the table but i want to see all the patients prescriptions rather than just ones for that appointment, but if one is added through the relationmanager it should be saved with the appointment id
toeknee
toeknee2w ago
add an additional where to the query imho..... add a new relationship all_prescriptions See: https://filamentphp.com/docs/2.x/admin/resources/relation-managers#deleting-records There is the getTableQuery method, just add a where('id', '>', 0) or modify it and remove the where record id.
chris.mccabe
chris.mccabeOP2w ago
@toeknee i tried this but i'm in filament 3 and i think adding the method is deprecated
toeknee
toeknee2w ago
On the $table instance in the source ->modifyQueryUsing(fn(Builder $query) => $query->where('appointment_id', '>', 1))
chris.mccabe
chris.mccabeOP2w ago
@toeknee i dont think you can overwrite the where clauses from the relationmanager- just add to them?
toeknee
toeknee7d ago
I am fairly sure you can, I haven't checked though. But adding ->modifyQueryUsing(fn(Builder $query) => $query->where('appointment_id', '>', 1)) To the relation manager table means it will find all above the id of 1 which basically means the id doesn't matter.
chris.mccabe
chris.mccabeOP7d ago
thanks for your responses, this is what i have added
])->modifyQueryUsing(function (Builder $query) {
$appointmentId = $this->ownerRecord->id;
$patientId = $this->ownerRecord->patient_id;

return $query->where(function ($query) use ($appointmentId) {
$query->where('appointment_id', $appointmentId)
->orWhereNull('appointment_id');
})->where('patient_id', $patientId);
});
])->modifyQueryUsing(function (Builder $query) {
$appointmentId = $this->ownerRecord->id;
$patientId = $this->ownerRecord->patient_id;

return $query->where(function ($query) use ($appointmentId) {
$query->where('appointment_id', $appointmentId)
->orWhereNull('appointment_id');
})->where('patient_id', $patientId);
});
and checked the debug bar you can see the query is
select count(*) as aggregate from "prescriptions" where "prescriptions"."appointment_id" = 1 and "prescriptions"."appointment_id" is not null and ("appointment_id" = 1 or "appointment_id" is null) and "patient_id" = 1
select count(*) as aggregate from "prescriptions" where "prescriptions"."appointment_id" = 1 and "prescriptions"."appointment_id" is not null and ("appointment_id" = 1 or "appointment_id" is null) and "patient_id" = 1
so it just appends where clauses on to the end of the existing generated query
toeknee
toeknee7d ago
])->modifyQueryUsing(function (Builder $query) {
$appointmentId = $this->ownerRecord->id;
$patientId = $this->ownerRecord->patient_id;

return $query->where(function ($query) use ($appointmentId) {
$query->where('appointment_id', '>', 0)
->orWhereNull('appointment_id');
})->where('patient_id', $patientId);
});
])->modifyQueryUsing(function (Builder $query) {
$appointmentId = $this->ownerRecord->id;
$patientId = $this->ownerRecord->patient_id;

return $query->where(function ($query) use ($appointmentId) {
$query->where('appointment_id', '>', 0)
->orWhereNull('appointment_id');
})->where('patient_id', $patientId);
});
That limits it to the appointment_id still.... but also null values change it to the above. I beleive the above is then right
chris.mccabe
chris.mccabeOP3d ago
thanks for that but updating it to your code has the same issue, the query ends up similar
select count(*) as aggregate from "prescriptions" where "prescriptions"."appointment_id" = 1 and "prescriptions"."appointment_id" is not null and ("appointment_id" > 0 or "appointment_id" is null) and "patient_id" = 1
select count(*) as aggregate from "prescriptions" where "prescriptions"."appointment_id" = 1 and "prescriptions"."appointment_id" is not null and ("appointment_id" > 0 or "appointment_id" is null) and "patient_id" = 1
@toeknee i managed to get this working- its disgusting but works:
php
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('medication.name')
->columns([
Tables\Columns\TextColumn::make('drug.brand_name'),
Tables\Columns\TextColumn::make('start_date'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make()->label('New Prescription'),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
])->modifyQueryUsing(function (Builder $query) {
$patientId = $this->ownerRecord->patient_id;

// Clear existing where clauses and bindings
$query->getQuery()->wheres = [];
$query->getQuery()->bindings['where'] = [];

return $query->where('patient_id', $patientId);
});
}
php
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('medication.name')
->columns([
Tables\Columns\TextColumn::make('drug.brand_name'),
Tables\Columns\TextColumn::make('start_date'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make()->label('New Prescription'),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
])->modifyQueryUsing(function (Builder $query) {
$patientId = $this->ownerRecord->patient_id;

// Clear existing where clauses and bindings
$query->getQuery()->wheres = [];
$query->getQuery()->bindings['where'] = [];

return $query->where('patient_id', $patientId);
});
}
Want results from more Discord servers?
Add your server