F
Filament11mo ago
Arjen

Table column loses data when sorting by relationship

I have the following relation Booking -> HasMany -> Dayparts where Dayparts has a field date. I want to sort courses by the first child in Dayparts and while the code for that sorts the items in the table correctly, the data in the sorted column in the table just disappears. When the table is not sorted, the date is showing correctly. Any idea what I'm doing wrong? This is the table column in the BookingResource:
Tables\Columns\TextColumn::make('dayparts.0.date')
->label('Datum')
->date('d-m-Y')
->sortable(
query: fn ($query, $direction) => $query
->join('dayparts', function ($join) {
$join->on('dayparts.booking_id', '=', 'bookings.id')
->on('dayparts.id', DB::raw("(SELECT min(id) FROM dayparts WHERE dayparts.booking_id = bookings.id)"));
})
->orderBy('date', $direction)
),
Tables\Columns\TextColumn::make('dayparts.0.date')
->label('Datum')
->date('d-m-Y')
->sortable(
query: fn ($query, $direction) => $query
->join('dayparts', function ($join) {
$join->on('dayparts.booking_id', '=', 'bookings.id')
->on('dayparts.id', DB::raw("(SELECT min(id) FROM dayparts WHERE dayparts.booking_id = bookings.id)"));
})
->orderBy('date', $direction)
),
I tried changing 'dayparts.0.date' to 'date' as well, but that changes nothing.
1 Reply
Arjen
ArjenOP10mo ago
I've got it working by using the following code:
Tables\Columns\TextColumn::make('booking_date')
->label('Datum')
->date('d-m-Y')
->sortable(
query: fn ($query, $direction) => $query
->join('dayparts', function ($join) {
$join->on('dayparts.booking_id', '=', 'bookings.id')
->on('dayparts.id', DB::raw("(SELECT min(id) FROM dayparts WHERE dayparts.booking_id = bookings.id)"));
})
->orderBy('date', $direction)
),
Tables\Columns\TextColumn::make('booking_date')
->label('Datum')
->date('d-m-Y')
->sortable(
query: fn ($query, $direction) => $query
->join('dayparts', function ($join) {
$join->on('dayparts.booking_id', '=', 'bookings.id')
->on('dayparts.id', DB::raw("(SELECT min(id) FROM dayparts WHERE dayparts.booking_id = bookings.id)"));
})
->orderBy('date', $direction)
),
Booking model:
public function getBookingDateAttribute()
{
return data_get($this, 'date', data_get($this->dayparts, '0.date', null));
}
public function getBookingDateAttribute()
{
return data_get($this, 'date', data_get($this->dayparts, '0.date', null));
}
I'm curious to know if this is an acceptable solution or if there are better solutions for this problem.
Want results from more Discord servers?
Add your server