LoginSignup
13
9

More than 5 years have passed since last update.

Laravel SQLSTATE[42S22]: 解決メモ

Last updated at Posted at 2019-01-11

モデルの新規保存でのエラー

「PHPフレームワーク Laravel入門」のP252のupdateメソッドを書いて実行した際に出たエラーの解決をメモする。

PersonController.php
public function add(Request $request)
{
    return view('person.add');
}

public function create(Request $request)
{
    $this->validate($request, Person::$rules);
    $person = new Person;
    $form = $request->all();
    unset($form['_token']);
    $person->fill($form)->save();
    return redirect('./person');
}

フォームを送信すると、

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list (SQL: insert into `people` (`name`, `mail`, `age`, `updated_at`, `created_at`)(以下省略)

とエラーが発生。調べたところ「updated_atカラム」が無いとのことです。
解決方法はタイムスタンプを無効にすれば良いようです。

PersonController.php
public function add(Request $request)
{
    return view('person.add');
}

public function create(Request $request)
{
    $this->validate($request, Person::$rules);
    $person = new Person;
    $form = $request->all();
    unset($form['_token']);
    $person->timestamps = false;    // 追記
    $person->fill($form)->save();
    return redirect('./person');
}

解決方法として良いのか悪いのかわからないがフォーム送信ができ、レコードが追加できたので良かった。

参考サイト

【Laravel】タイムスタンプがないテーブルを更新(保存)する方法 | 84LIFE

13
9
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
9