php - How to default order results of belongsTo relationship? -
//model user class user extends eloquent { public function post() { return $this>hasmany('post'); } } //model post class post extends eloquent { public function user() { return $this->belongsto('user'); } }
taken laravel documentation:
since, eloquent models themselves, relationships serve powerful query builders, defining relationships functions provides powerful method chaining , querying capabilities.
so, this:
class user extends model { public function posts() { return $this->hasmany('app\post')->orderby('field_name'); } }
Comments
Post a Comment