2017-06-16 64 views
-1
public function totalArticles(){ 
    return $this->updates()->count() + $this->events()->count(); 
}} 

這裏是我的模型,我想使用orderBy爲了這個定單從最小到最大。如何在關係laravel中使用orderBy?

+2

$結果= MODEL_NAME ::排序依據( 'ID', 'ASC') - >獲得(); – JYoThI

+0

public function totalArticles(){ return $ this-> hasMany('.....') - > orderBy('tablename.columnname'); } – user3663

+1

可能的重複https://stackoverflow.com/questions/18143061/laravel-orderby-on-a-relationship –

回答

1

如果你想獲得CREATED_DATE爲了記錄

升序:

$result = ModelName::latest(); 

降序:

$result = ModelName::oldest(); 

除了日期

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