Conditional fill of Json data

I have a table ipd_charges like




id doctor_name charges_cash charges_cashless

1      1             300         600

2      2             200         400



and in my controller the code is like this which is working fine




public function actionCharges($id){

    $model= \app\models\IpdCharges::findOne(['doctor'=>$id]);

    return \yii\helpers\Json::encode([

        'visit_charges'=>$model->charges_cash,

    ]);

    }



Now what I want is to fill the data on condition and tried to modify the above code which is only returning the correct result on condition check.




public function actionCharges($id){

    $model= \app\models\IpdCharges::findOne(['doctor'=>$id]);

    $query = (new \yii\db\Query())

    ->select('tpa_name')

    ->from('patient_detail') 

    ->innerJoin('daily_ward_entry', 

   'daily_ward_entry.general_regn_no = patient_detail.general_regn_no')

    ->where(['daily_ward_entry.id'=>$id]);


   $command = $query->createCommand();

   $rows = $command->queryAll();





 if ($rows['tpa_name'] === NULL){

    return \yii\helpers\Json::encode([      

        'visit_charges'=>$model->charges_cash,        

    ]);

    }else {

       return \yii\helpers\Json::encode([         

        'visit_charges'=>$model->charges_cashless,

            ]);

       }

    }

 



I am looking for a little help in this direction. Thank you in advance.