Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Josadec's avatar

Help, I have issues with try calculate column sortBy ASC or DESC

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'));
}
0 likes
4 replies
jlrdw's avatar

dd some of these and see what you have.

Josadec's avatar

@jlrdw I did that multiple times, or where do you think is where I need to check? because I'm stuck

Josadec's avatar

@jlrdw Sure, this my funtion make dinamic sort with my var $calculatedSortField, $calculatedSortDirection

    public $calculatedSortField = 'days_left_remainding';
    public $calculatedSortDirection = 'desc';
  public function sortByCalculated($field)
    {
        if ($this->calculatedSortField === $field) {
            $this->calculatedSortDirection = $this->calculatedSortDirection === 'asc' ? 'desc' : 'asc';
        } else {
            $this->calculatedSortDirection = 'asc';
        }
        $this->calculatedSortField = $field;
    }

Blade view

 <th class="
px-2 py-3 border-b-2 border-gray-200 dark:border-slate-500 text-left text-xs font-semibold dark:text-gray-400 text-gray-700 uppercase tracking-wider
">
 <button wire:click="sortByCalculated('days_left_remainding')" class="uppercase flex items-center hover:underline">
       Days Left                                                      
          @if ($calculatedSortField === 'days_left_remainding')
                   <svg class="
w-5 h-5 ml-1 duration-200 @if ($sortDirection === 'desc') rotate-180 @endif" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" >
                   <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
           </svg>
        @endif
      </button>
</th>

Please or to participate in this conversation.