Laravel code for order Asc and Desc of Custom Filed in Yagra Datatables

    public function query(Customer $model)

        {
        $query = $model->newQuery()
            ->select('*', \DB::raw('(amount1- amount2) as amount'))
            ->where('amount1', '>', 0);          

        // Check if a specific column is being sorted
        if ($this->request()->has('order.0.column')) {
            $columnIndex = $this->request()->input('order.0.column');
            $orderb = $this->request()->input('order.0.dir');
                 
            $column = $this->request()->input("columns.$columnIndex.data");
           
            //If the clicked column is 'amount', order by it; otherwise, no custom ordering
            if ($column !== 'amount') {
                // Reset ordering if clicked on a different column
                $query->orderBy($column, $orderb);
            }else{
                $query->orderBy('amount', $orderb);
            }

        } else {
            // Initial/default ordering
            $query->orderBy('amount', 'desc');
        }

        return $query;
        }  

No comments:

Post a Comment

Pages