0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravelでデータベースのcreated_at, updated_atの自動更新を無効化する

Posted at

新規登録機能を実装するとき、usersテーブル、menテーブル(男性), womenテーブル(女性)テーブルを作成して、ユーザー情報を保存するときに男性、 女性で保存する情報を変えるためにテーブルを分けました。
このときにusersテーブルのupdated_at . created_atで更新、 作成日を管理できるので, men ・ womenテーブルにはupdated_at・created_atカラムを作成しませんでした。
men. womenにデータを保存する処理を行ったときに下記ののようなエラーが発生しました。

エラー内容

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list'

テーブルにupdated_atカラムがないですよってエラーが発生。

解決用法

app/Man.php
public $timestamps = false;

モデル内でタイムスタンプをfalseにすると解決。
Eloquentはデフォルトだとupdated_at, created_atを自動更新するので自動更新をしたくない場合はここをfalseに変えてあげれば自動更新を止める事ができます。

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?