如何在Datatable中订购自定义属性?

时间:2018-07-30 08:09:59

标签: backpack-for-laravel

假设我的用户模型的balance属性getBalanceAttribute()从用户的交易模型返回amount的总和,这在数据表中如何排序?

User.php

public function transactions()
{
  return $this->hasMany(\App\Transaction::class);
}

public function getBalanceAttribute()
{
  return $this->transactions()->sum('amount');
}

Transaction.php

public function user()
{
  return $this->belongsTo(\App\User::class);
}

UserCrudController.php

...
public function setup()
{
  ...
  $this->crud->addColumn(
  [
    'name' => "balance",
    'label' => "Balance",
    'type' => 'number',
    // Here the column is clickable but is not actually sorted.
    'orderable' => true,
  ],
  ...
}

提前谢谢!

1 个答案:

答案 0 :(得分:0)

不幸的是,背包无法使#!/usr/bin/python import os import csv lines = open('FINOPEXORG-FINOPEXORG_200.dat', 'r').readlines() output = open("sorted.dat", 'w') for line in sorted(lines,key=int(lambda line:line.split('|')[6])): output.write(line) output.close() 列可排序,因为模型函数是在SQL恢复后调用的。

对不起。