LaravelのEloquentを用いたデータ更新時の更新日時について
前提・実現したいこと
Laravel5.5を使って、システム開発を行っています。
対象のテーブルに対し、Eloquentを使ってデータ更新を行っています。
テーブルには created_at と updated_at という2つのカラムがあるのですが、データ更新時にはupdated_atのみ日時を更新してほしいです。
発生している問題・エラーメッセージ
データ更新時にはupdated_atのみ日時を更新してほしいですが、created_atも同じ日時で更新されてしまいます。
試したこと
各定義およびソースは、以下の通りです。
◆テーブル定義
CREATE TABLE `account` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `account_name` varchar(255) NOT NULL, `account_name_kana` varchar(255) NOT NULL, `mail_address` varchar(255) NOT NULL, `phone_number` varchar(20) NOT NULL, `version` bigint(20) unsigned NOT NULL DEFAULT '1', `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `created_user_id` bigint(20) unsigned NOT NULL DEFAULT '0', `updated_at` timestamp NULL DEFAULT NULL, `updated_user_id` bigint(20) unsigned NOT NULL DEFAULT '0', `deleted_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
◆モデル
<?php namespace App\Models; class Account extends AppModel { protected $table = 'account'; protected $primaryKey = 'id'; }
◆継承モデル
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class AppModel extends Model { use SoftDeletes; }
◆データ更新部分
$account = Account::find($account_id); $account->account_name = $request->input('account_name'); $account->account_name_kana = $request->input('account_name_kana'); $account->mail_address = $request->input('mail_address'); $account->phone_number = $request->input('phone_number'); $account->updated_user_id = $this->account_id; $account->increment('version', 1); $account->save();
補足情報(FW/ツールのバージョンなど)
◆動作環境
・Windows10 Pro 64bit
・Apache2.4
・PHP7.1
・Laravel5.5
・MySQL5.7
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/23 23:25