chris.mccabe
chris.mccabe
FFilament
Created by chris.mccabe on 11/13/2024 in #❓┊help
Relation manager pulls wrong data
@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);
});
}
17 replies
FFilament
Created by chris.mccabe on 11/13/2024 in #❓┊help
Relation manager pulls wrong data
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
17 replies
FFilament
Created by chris.mccabe on 11/13/2024 in #❓┊help
Relation manager pulls wrong data
so it just appends where clauses on to the end of the existing generated query
17 replies
FFilament
Created by chris.mccabe on 11/13/2024 in #❓┊help
Relation manager pulls wrong data
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
17 replies
FFilament
Created by chris.mccabe on 11/13/2024 in #❓┊help
Relation manager pulls wrong data
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);
});
17 replies
FFilament
Created by chris.mccabe on 11/13/2024 in #❓┊help
Relation manager pulls wrong data
@toeknee i dont think you can overwrite the where clauses from the relationmanager- just add to them?
17 replies
FFilament
Created by chris.mccabe on 11/13/2024 in #❓┊help
Relation manager pulls wrong data
@toeknee i tried this but i'm in filament 3 and i think adding the method is deprecated
17 replies
FFilament
Created by chris.mccabe on 11/13/2024 in #❓┊help
Relation manager pulls wrong data
@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
17 replies
FFilament
Created by chris.mccabe on 11/13/2024 in #❓┊help
Relation manager pulls wrong data
you are correct i had the wrong model class in my relation on the Appointment Model
17 replies