我已将 laravel dataTable 实现为服务。 最初的两列是实际的 id 和名称,因此我可以在表格呈现后对其进行升序/降序排序。 但是接下来的几列在执行少量计算后呈现,即这些值不是直接从任何列获取而是经过处理。 我无法对执行计算的这些列进行排序,并且收到此错误。我知道它正在寻找特定的列,例如我在数据库中没有的outstanding_amount,而是从其他表中的两个或更多列计算出的金额。 关于如何克服这个问题有什么建议吗?
最佳答案
看起来您正在尝试按不是列的值而是计算值进行排序。 所以这里的主要问题是为 Eloquent/MySql 提供提供排序所需的数据。
// You might need to do some joins first
->addSelect(DB::raw('your_calc as outstanding_amount'))
->orderBy('outstanding_amount') // asc can be omitted as this is the default
// Anternative: you don't need the value sorted by
// Don't forget any joins you might need
->orderByRaw('your_calc_for_outstanding_amount ASC')
关于laravel - Laravel DataTables 作为服务实现中表渲染后的排序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51168452/