Eloquent Relationship with 2 other Models and orderBy?
hi,
i have an event model and 2 related other models (tasks, states)
how can i make the state model is orderedBy name and the states model is ordered by id?
public function states()
{
return $this->hasMany(State::class)->orderBy('name','asc');
}
public function tasks()
{
return $this->hasMany(Task::class)->orderBy('id','asc');
}
Just guessing at the models and relationships, hopefully you get the idea
Thnx snapey, this is one solution, but in this case every that order will be that way every time, i fetch the models through the parent one.
I think mightylal way is more flexible.
Thank you both.
public function states()
{
return $this->hasMany(State::class);
}
public function statesByName()
{
return $this->hasMany(State::class)->orderBy('name','asc');
}