How can I sort by a custom Column
Here's the table data, which uses Account, Customer and Employee models:
Account::query()
->with([customer, employee])
Here are the relationships in the Account model
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class, 'contactid');
}
public function employee(): BelongsTo
{
return $this->belongsTo(Employee::class, 'contactid');
}
...and the aggregated attribute:
public function getCombinedNameAttribute()
{
if ($this->contactType == 'Customer'){
return $this->customer->name
}
elseIf ($this->contactType == 'Employee'){
return $this->employee->fullname
}
}
The text column works fine, but I just can't make it sortable. The code below doesn't seem to work:
TextColumn::make('combined_name')
->sortable()
Account::query()
->with([customer, employee])
Here are the relationships in the Account model
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class, 'contactid');
}
public function employee(): BelongsTo
{
return $this->belongsTo(Employee::class, 'contactid');
}
...and the aggregated attribute:
public function getCombinedNameAttribute()
{
if ($this->contactType == 'Customer'){
return $this->customer->name
}
elseIf ($this->contactType == 'Employee'){
return $this->employee->fullname
}
}
The text column works fine, but I just can't make it sortable. The code below doesn't seem to work:
TextColumn::make('combined_name')
->sortable()