To get the model with the maximum id you can apply the reverse order and restriction to one.
SystemStatisticsHistory::find()->orderBy(['id' => SORT_DESC])->one();
Another option is to use a subselect with max as follows:
SystemStatisticsHistory::find() ->where(['id' => SystemStatisticsHistory::find()->max('id')]) ->one();
There are some nuances using the latter option, check this question .
You can check the documentation for max() here .
I personally prefer to use the first option.
To get the first record, simply change the direction of the order to SORT_ASC in the first request and from max() to min() in the second request.
PS Encoded id is bad practice.
source share