Search code examples
phpyii2yii2-advanced-appyii2-model

How to add orderBy() with where() of Find()?


How to add an orderBy() with a where condition?

$floor_data = Floors::find()->where(['building_id' => $id])->orderBy->(['floor_no' => SORT_DESC])->all();

This is giving me a syntax error saying

syntax error, unexpected '(', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'


Solution

  • The extra -> caused the error:

    $floor_data = Floors::find()
        ->where(['building_id' => $id])
        //   Remove ↓↓   
        // ->orderBy->(['floor_no' => SORT_DESC])
        ->orderBy(['floor_no' => SORT_DESC])
        ->all();