1
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 5 years have passed since last update.

【Laravel】updated_atカラムがないテーブルをEloquentモデルを用いて更新するときの注意点

Posted at

備忘録として残します。

Eloquentモデルを用いて更新をかけるとき下記のような記述をすると思います。
HogeTable::find($id)->fill($input)->save();
このときLaravelはEloquentモデルで更新しようとしたとき、暗黙的にupdated_atというカラムも同時に更新しようとします。

しかし、updated_atというカラムがない場合は、エラーをはくので、この暗黙的にタイムスタンプをする機能をオフにする方法が下記になります。

■やり方


namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class HogeTable extends Model
{
    public $timestamps = false; // この行をEloquentモデルに追記
    protected $table = 'hoge';
}
1
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
1
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?