问题是:
怎样才能用添加的属性 total 进行排序
场景:
Users表
ID | 姓名 | 语言 | 数学 | 英语
-----------------------------------------
1 | 张三 | 99 | 78 | 66
-----------------------------------------
2 | 李四 | 50 | 100 | 87
-----------------------------------------
3 | 王五 | 88 | 91 | 87
-----------------------------------------
添加的属性 total
class User extends Model
{
protected $appends = ['total'];
public function getTotalAttribute () {
$yuwen = $this->attributes['yuwen'];
$shuxue = $this->attributes['shuxue'];
$yingyu = $this->attributes['yingyu'];
return $yunwen + $shuxue + $yingyu;
}
}
如果用语文的分数排序时是:
class UserController extends Controller
{
public function userListApi()
{
$datas = User::orderBy('yuwen', 'dsec')->paginate(10);
return response()->json($datas);
}
}
怎么样才能让添加的属性 total 参与排序呢?