F
Filamentβ€’2y ago
toeknee

relationship with trashed

When using relationship() how do we handle loading trashed items into the field? excample:
CheckboxList::make('teams')
->label('Assigned Team(s)')
->required()
->relationship('teams', 'name',
function (Builder $query) {
return $query->select(['id', \DB::Raw('CONCAT(name, \' - \', custom_field) as name')]);
}
)
->reactive()
->columnSpanFull(),
CheckboxList::make('teams')
->label('Assigned Team(s)')
->required()
->relationship('teams', 'name',
function (Builder $query) {
return $query->select(['id', \DB::Raw('CONCAT(name, \' - \', custom_field) as name')]);
}
)
->reactive()
->columnSpanFull(),
12 Replies
Patrick Boivin
Patrick Boivinβ€’2y ago
If I'm reading this right, would this depend on how you generate $allTeams?
toeknee
toekneeOPβ€’2y ago
Ahh no, that makes no difference. It was then from being a standard non-relationship select Updated, it's the relationship which applies withoutTrashed by default
LeandroFerreira
LeandroFerreiraβ€’2y ago
Did you try ->withTrashed()->get()... ?
toeknee
toekneeOPβ€’2y ago
Can't use ->get() in the builder. But I tried ->withTrashed() on the query->select i.e.
return $query->select(['id', \DB::Raw('CONCAT(name, \' - \', custom_field) as name')])->withTrashed();
return $query->select(['id', \DB::Raw('CONCAT(name, \' - \', custom_field) as name')])->withTrashed();
Patrick Boivin
Patrick Boivinβ€’2y ago
Do you still get a result here or some error? ^
toeknee
toekneeOPβ€’2y ago
Nothing, it just doesn't apply
Patrick Boivin
Patrick Boivinβ€’2y ago
Ok... Not sure but I think using ->select() messes with the available columns in the query... so you might have to do the equivalent of withTrashed() manually, by checking the deleted_at column
toeknee
toekneeOPβ€’2y ago
Ok thanks! I'll have a play a bit more Thanks Guys, so ->withTrashed() worked but my CONCAT was only working when custom_field had a value πŸ™‚
Patrick Boivin
Patrick Boivinβ€’2y ago
Oh, cool! Did you modify the DB::raw() in any way? (just curious)
toeknee
toekneeOPβ€’2y ago
Yep, I chucked an IFNULL in so:
return $query->select(['id', \DB::Raw('CONCAT(name, \' - \', IFNULL(custom_field, \'\')) as name')])->whereNotNull('name')->withTrashed();
return $query->select(['id', \DB::Raw('CONCAT(name, \' - \', IFNULL(custom_field, \'\')) as name')])->whereNotNull('name')->withTrashed();
LeandroFerreira
LeandroFerreiraβ€’2y ago
what about this?
->relationship('teams', 'name', function (Builder $query) {
return $query->withTrashed();
})
->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->name} - {$record->custom_field}")
->relationship('teams', 'name', function (Builder $query) {
return $query->withTrashed();
})
->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->name} - {$record->custom_field}")
toeknee
toekneeOPβ€’2y ago
Ohhhh Leandro! That's mucho better. I forgot about option label
Want results from more Discord servers?
Add your server