简体   繁体   中英

How to use orderBy in relationship laravel?

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

here is my model and i wanna use orderBy for this to order form min to max.

If you want get records order by created_date

Ascending:

$result = ModelName::latest();

Descending:

$result = ModelName::oldest();

Other than date

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM