Noob question: Sorting a hasMany column in table

I'm really struggle sorry and I am sure it is dead easy to do. Apologies, I know I've asked a bunch of relation questions, but that seems to be where stuff explodes for me. What I am trying to do: I need the HIndex column in the Activities table to be sortable based on the related HIndex with the highest year value (or based on the formatted state would be even better) Model Activity: - has many HIndex via
public function hIndices()
{
return $this->hasMany(HIndex::class)->orderBy('year', 'desc');
}
public function hIndices()
{
return $this->hasMany(HIndex::class)->orderBy('year', 'desc');
}
Model HIndex : - Belongs to one Activity - Has the columns -- year -- value -- activity_id In the Activity resource I have:
TextColumn::make('hIndices.value')
->formatStateUsing(function ($record) {
// ... some stuff dont worry about it
}
->sortable(),
TextColumn::make('hIndices.value')
->formatStateUsing(function ($record) {
// ... some stuff dont worry about it
}
->sortable(),
But it throws: SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row
1 Reply
403gtfo
403gtfo2mo ago
I've tried
public function latestHIndex()
{
return $this->hasOne(HIndex::class)
->orderBy('year', 'desc');
}
public function latestHIndex()
{
return $this->hasOne(HIndex::class)
->orderBy('year', 'desc');
}
and
TextColumn::make('latestHIndex.value')->sortable(),
TextColumn::make('latestHIndex.value')->sortable(),
It displays right but when I try to sort it it just throws
SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row
SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row