0
1

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のEloquentモデルでupdated_atがないテーブルを使う方法

Posted at

Eloquentモデルは作成時に自動でcreated_atとupdated_atを更新してくれるようになっているので、updated_atがないテーブルでデータを登録しようとすると、こんな感じで怒られる。

Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: insert into `positions` (`name`, `order_number`, `updated_at`, `created_at`) values (test, 17, 2020-08-22 07:58:07, 2020-08-22 07:58:07))
http://localhost/positions/create

updated_atテーブルがないテーブルで登録したい場合はデータクラスに以下を追加すると、created_atとupdated_atの更新を無効にできます。

Positions.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Position extends Model
{
    public $timestamps = false; // 追加
}
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?