Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad Request (#400) - Missing required parameters: id in YII2

I want to make a CRUD operation using GII Tool, but I get the error message Missing required parameters: id, when I try to save my post.

Post controller:

public function actionCreate()
{
    $model = new Post();

    if ($model->load(Yii::$app->request->post())) {
        $model->post_create_time=date('Y-m-d h:m:s');
        $model->save();
        return $this->redirect(['view', 'id' => $model->id_post]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

Why do I always get this error?

like image 847
Shinoda_ Avatar asked Feb 21 '15 18:02

Shinoda_


Video Answer


1 Answers

Try

public function actionCreate()
{
    $model = new Post();

    if ($model->load(Yii::$app->request->post())) {
        $model->post_create_time=date('Y-m-d h:m:s');
        $model->save(false);

        return $this->redirect(['view', 'id' => $model->id_post]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

Make sure that you do $model->save(false) and see whether if it works.

like image 184
Abhimanyu Saharan Avatar answered Sep 28 '22 14:09

Abhimanyu Saharan