如何在关系laravel中使用orderBy?

时间:2017-06-16 04:05:38

标签: php laravel

public function totalArticles(){
    return $this->updates()->count() + $this->events()->count();
}}

这是我的模型,我想使用orderBy来订购从最小到最大的形式

1 个答案:

答案 0 :(得分:1)

  

如果您想通过created_date获取记录订单

<强>升序:

$result = ModelName::latest();

降序:

$result = ModelName::oldest();
  

除了日期

$result = ModelName::orderBy('id', 'ASC')->get();  // ascending
$result = ModelName::orderBy('id', 'DESC')->get(); // descending