Order by rand() in yii2

Order by rand() in yii2

use yii\db\Expression;

$query = User::find()
    ->where(['category_id' => 5])
    ->orderBy(new Expression('rand()'))
    ->limit(4);

Leave a Reply

Your email address will not be published. Required fields are marked *