Level 75
dd some of these and see what you have.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Good evening everyone I'm encountering issues when attempting to sort data by a calculated column. I'm not receiving any errors, but the data remains unsorted.
public $calculatedSortField = 'days_left_remainding';
public $sortDirection = 'desc';
Function sortByCalculated($field)
public function sortByCalculated($field)
{
if ($this->calculatedSortField === $field) {
$this->calculatedSortDirection = $this->calculatedSortDirection === 'asc' ? 'desc' : 'asc';
} else {
$this->calculatedSortDirection = 'asc';
}
$this->calculatedSortField = $field;
}
Function render
public function render()
{
$holidays = Cache::remember('holidays', 3600, function () {
return Holiday::pluck('date')->toArray();
});
$estimates = $this->queryWithFilter()
->orderBy($this->sortField,$this->sortDirection)
->paginate($this->perPage);
$estimates->map(function ($estimate) use ($holidays){
$estimate->daysLeft = Estimator::daysLeft($estimate, $holidays);
$estimate->averageTime = Estimator::averageTimeBetweenTwoDaysEstimate($estimate,$holidays);
$estimate->daysLeftRemainding = Estimator::daysLeftRemainding($estimate,$holidays);
return $estimate;
});
if (in_array($this->calculatedSortField, ['days_until_bid'])) {
$estimates->setCollection($estimates->getCollection()->sortBy($this->calculatedSortField, SORT_REGULAR,
$this->sortDirection === 'desc'));
}
$branches = Cache::remember('branches',3600,function(){
return Branch::select('id', 'name', 'short_name')->orderBy('short_name')->get();
});
$stages = Cache::remember('stages',3600,function(){
return Stage::select('id', 'name', 'color')->orderBy('name')->get();
});
$users = Cache::remember('dailies',3600,function (){
return Daily::distinct()->select('done_by')->orderBy('done_by')->get();
});
return view('livewire.estimates.index',compact('estimates','branches','stages','users'));
}
Please or to participate in this conversation.