Sort hasMany relation in Yii2
Setting order directly in relation may not be reliable in particular cases. So you can set order in AR query
Device::find()
->where(['id' => $id])
->with('prices' => function(\yii\db\ActiveQuery $query) {
$query->orderBy('device_price DESC');
})
->one();
I think you can assign the order by directly in relation
public function getPrices()
{
return $this->hasMany(Prices::className(), ['device_id' => 'id'])->
orderBy(['device_price' => SORT_DESC]);
}