public function actionCreate($book_id = 'book_id')
{
$model = new Books();
// check if post request
if ( $model->load(Yii::$app->request->post()) ) {
// check if book_id exists in table
if ( ! Books::find()->where(['book_id' => $book_id])->one() ) {
// save new record to model
if ( ! $model->save() ) {
// set error message and redirect to 'create' page
Yii::$app->session->setFlash('Error','There was some error in processing your request');
return $this->redirect(Yii::$app->request->referrer);
}
// if model is saved successfully, redirect to 'view' page
Yii::$app->session->setFlash('Success','You have successfully borrowed the book');
return $this->redirect(['view', 'book_id' => $model->book_id]);
} else {
// if book_id exist in table, show error message
Yii::$app->session->setFlash('error', 'The book has been borrowed, Please look for another one.');
return $this->redirect(Yii::$app->request->referrer);
}
}
return $this->render('create', [
'model' => $model,
]);
}
Всегда лучше проверить условие «ложь» в операторах if, чем в true. Помогает нам писать более чистый код. Также произошла синтаксическая ошибка в вашем перенаправлении для просмотра оператора