{"id":7646,"date":"2020-08-10T15:00:39","date_gmt":"2020-08-10T06:00:39","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=7646"},"modified":"2020-08-06T02:48:28","modified_gmt":"2020-08-05T17:48:28","slug":"%e3%80%90laravel%e3%80%91db%e6%93%8d%e4%bd%9c%e3%81%aerevert%ef%bc%88%e6%99%82%e3%82%92%e6%88%bb%e3%81%9d%e3%81%86%ef%bc%89%e6%a9%9f%e8%83%bd%e3%82%92%e3%81%a4%e3%81%8f%e3%82%8b","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=7646","title":{"rendered":"\u3010Laravel\u3011DB\u64cd\u4f5c\u306erevert\uff08\u6642\u3092\u623b\u305d\u3046\uff09\u6a5f\u80fd\u3092\u3064\u304f\u308b"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u3053\u308c\u306f\u79c1\u306b\u9650\u3089\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u4f55\u6c17\u306a\u304f\u904e\u3054\u3057\u3066\u3044\u308b\u3068\u300c<strong>\u305d\u3046\u3044\u3048\u3070\u30a2\u30ec\u9762\u767d\u304b\u3063\u305f\u306a\u301c<\/strong>\u300d\u3068\u30af\u30b9\u3063\u3068\u3057\u3066\u3057\u307e\u3063\u305f\u308a\u3057\u307e\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u5148\u65e5\u306f\u305d\u306e\u5f71\u97ff\u3067<code>Laravel<\/code>\u3067\u3064\u304f\u308a\u305f\u3044\u3042\u308b\u6a5f\u80fd\u3092\u601d\u3044\u3064\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u306e\u6a5f\u80fd\u3068\u306f\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb<\/p>\n<p><strong style=\"font-size: 35px;\">revert \u6a5f\u80fd\u00a0<\/strong><\/p>\n<p>\u3067\u3059\u3002<\/p>\n<p><code>revert<\/code>\u3068\u306f\u3001\u7c21\u5358\u306b\u8a00\u3046\u3068\u30c7\u30fc\u30bf\u3092\u3042\u308b\u7279\u5b9a\u306e\u6642\u70b9\u306b\u623b\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u6a5f\u80fd\u306e\u3053\u3068\u3067\u3001\u304a\u7b11\u3044\u30b3\u30f3\u30d3\u300c<strong>\u307a\u3053\u3071<\/strong>\u300d\u3055\u3093\u304c\u6f2b\u624d\u306e\u4e2d\u3067\u4f7f\u3063\u3066\u3044\u305f\u300c<strong>\u6642\u3092\u623b\u305d\u3046\uff01<\/strong>\u300d\u3068\u3044\u3046\u30d5\u30ec\u30fc\u30ba\u304b\u3089\u30a2\u30a4\u30c7\u30a2\u304c\u6d6e\u304b\u3073\u307e\u3057\u305f\ud83d\ude02\uff08\u6f2b\u624d\u9762\u767d\u304b\u3063\u305f\u3067\u3059\uff01\uff09<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f<strong>\u300c\u30c7\u30fc\u30bf\u3092\u3044\u3064\u306e\u72b6\u614b\u306b\u3067\u3082\u623b\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u300d<code>revert\u6a5f\u80fd<\/code>\u3092\u3064\u304f\u3063\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/strong><\/p>\n<p>\u305c\u3072\u7686\u3055\u3093\u306e\u304a\u5f79\u306b\u7acb\u3066\u308b\u3068\u5b09\u3057\u3044\u3067\u3059\ud83d\ude0a\u2728<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3566 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_1.png\" alt=\"\" width=\"306\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_1.png 306w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_1-287x300.png 287w\" sizes=\"auto, (max-width: 306px) 100vw, 306px\" \/>\u300c\u5916\u51fa\u308c\u306a\u3044\u3068\u3001YouTube\u3070\u3063\u304b\u898b\u3061\u3083\u3044\u307e\u3059\u306d\ud83d\ude02\u300d<\/p>\n<p><strong>\u958b\u767a\u74b0\u5883\uff1a<\/strong> Laravel 7.x<\/p>\n<h1>\u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>\u4eca\u56de\u306e\u958b\u767a\u3067\u5b9f\u88c5\u3059\u308b\u7d30\u304b\u306a\u5185\u5bb9\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<ul>\n<li>\u30c7\u30fc\u30bf\u304c\u4f5c\u6210\u3001\u66f4\u65b0\u3001\u524a\u9664\u3055\u308c\u308b\u305f\u3073\u306b\u5c65\u6b74\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3057\u3066\u3044\u304f<\/li>\n<li>\u30c7\u30fc\u30bf\u304c\u524a\u9664\u3055\u308c\u3066\u3082\u5143\u306b\u623b\u305b\u308b\u3088\u3046\u306b\u3059\u308b<\/li>\n<li>\u3069\u306e\u30e2\u30c7\u30eb\u3067\u3082\u6c4e\u7528\u7684\u306b\u3064\u304b\u3048\u308b\u3088\u3046\u306b\u3059\u308b<\/li>\n<li>\u7ba1\u7406\u3057\u3084\u3059\u3044\u3088\u3046\u306b\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u3064\u3051\u308b<\/li>\n<\/ul>\n<p>\u3067\u306f\u3084\u3063\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u2757<\/p>\n<h1>\u7de8\u96c6\u5c65\u6b74\u3092\u4fdd\u5b58\u3059\u308b\u30c6\u30fc\u30d6\u30eb\u3092\u3064\u304f\u308b<\/h1>\n<p>\u307e\u305a\u5c65\u6b74\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3057\u3066\u304a\u304f\u300c<strong>histories<\/strong>\u300d\u30c6\u30fc\u30d6\u30eb\u3068\u305d\u306e\u30e2\u30c7\u30eb\u3092\u3064\u304f\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>php artisan make:model History -m<\/strong><\/pre>\n<p>\u3059\u308b\u3068\u3001\u30e2\u30c7\u30eb\u3068\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u304c\u4e00\u6c17\u306b\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002<br \/>\n\u307e\u305a\u306f\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u8a2d\u5b9a\u3092\u3057\u307e\u3059\u3002<\/p>\n<p><strong>database\/migrations\/****_**_**_******_create_histories_table.php<\/strong><\/p>\n<pre>\/\/ \u7701\u7565\r\n\r\npublic function up()\r\n{\r\n    <strong>Schema::create('histories', function (Blueprint $table) {<\/strong>\r\n<strong>        $table-&gt;id();<\/strong>\r\n<strong>        $table-&gt;string('model');<\/strong>\r\n<strong>        $table-&gt;unsignedBigInteger('model_id');<\/strong>\r\n<strong>        $table-&gt;integer('version');<\/strong>\r\n<strong>        $table-&gt;string('type'); \/\/ `created`, `updated`, `deleted` or `reverted`<\/strong>\r\n<strong>        $table-&gt;text('data')-&gt;nullable(); \/\/ JSON<\/strong>\r\n<strong>        $table-&gt;timestamps();<\/strong>\r\n<strong>    });<\/strong>\r\n}\r\n\r\n\/\/ \u7701\u7565<\/pre>\n<p>\u3053\u306e\u4e2d\u3067\u8a2d\u5b9a\u3057\u3066\u3044\u308b\u30d5\u30a3\u30fc\u30eb\u30c9\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<ul>\n<li>model\uff1a \u5bfe\u8c61\u306e\u30e2\u30c7\u30eb<\/li>\n<li>model_id\uff1a \u5bfe\u8c61\u30e2\u30c7\u30eb\uff08\u30c6\u30fc\u30d6\u30eb\uff09\u306eID<\/li>\n<li>version\uff1a \u5c65\u6b74\u30d0\u30fc\u30b8\u30e7\u30f3<\/li>\n<li>type\uff1a \u3069\u3093\u306a\u64cd\u4f5c\u3092\u3057\u305f\u304b\u304c\u5206\u304b\u308b\u64cd\u4f5c\u30bf\u30a4\u30d7<\/li>\n<li>data\uff1a \u5bfe\u8c61\u30c7\u30fc\u30bf\uff08JSON\u5316\u3057\u3066\u4fdd\u5b58\uff09<\/li>\n<\/ul>\n<p>\u3067\u306f\u3001\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u304c\u5b8c\u6210\u3057\u305f\u3089\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u5b9f\u884c\u3057\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>php artisan migrate<\/strong><\/pre>\n<p>\u30c6\u30fc\u30d6\u30eb\u306f\u3053\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7660\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_9.png\" alt=\"\" width=\"350\" height=\"315\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_9.png 350w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_9-300x270.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>\u7d9a\u3044\u3066\u30e2\u30c7\u30eb\u306e\u8a2d\u5b9a\u3067\u3059\u3002<\/p>\n<p><strong>app\/History.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace App;\r\n\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n\r\nclass History extends Model\r\n{\r\n    <strong>protected $casts = [<\/strong>\r\n<strong>        'data' =&gt; 'json'<\/strong>\r\n<strong>    ];<\/strong>\r\n\r\n    \/\/ Others\r\n    <strong>public function revert() {<\/strong>\r\n\r\n<strong>        $self = $this; \/\/ use() \u306b\u306f $this \u3092\u4f7f\u3048\u306a\u3044\u306e\u3067\u5225\u306e\u5909\u6570\u3078\u683c\u7d0d<\/strong>\r\n\r\n<strong>        Model::withoutEvents(function() use($self){ \/\/ \u3053\u306e\u4e2d\u306f\u30a4\u30d9\u30f3\u30c8\u304c\u5b9f\u884c\u3055\u308c\u306a\u3044<\/strong>\r\n\r\n<strong>            \/\/ \u30c7\u30fc\u30bf\u3092\u5143\u306b\u623b\u3059<\/strong>\r\n<strong>            $model = (new $self-&gt;model())-&gt;firstOrNew(['id' =&gt; $self-&gt;model_id]);<\/strong>\r\n\r\n<strong>            foreach($self-&gt;data as $key =&gt; $value) {<\/strong>\r\n\r\n<strong>                $model-&gt;{$key} = (is_array($value)) ? json_encode($value) : $value;<\/strong>\r\n\r\n<strong>            }<\/strong>\r\n\r\n<strong>            $model-&gt;save();<\/strong>\r\n\r\n<strong>            \/\/ \u65b0\u3057\u3044\u5c65\u6b74\u3092\u8ffd\u52a0<\/strong>\r\n<strong>            $history = $self-&gt;replicate(); \/\/ \u5c65\u6b74\u30c7\u30fc\u30bf\u3092\u8907\u88fd<\/strong>\r\n<strong>            $history-&gt;version = $self-&gt;getNextVersionNumber($history-&gt;model, $history-&gt;model_id);<\/strong>\r\n<strong>            $history-&gt;type = 'reverted';<\/strong>\r\n<strong>            $history-&gt;save();<\/strong>\r\n\r\n<strong>        });<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n\r\n<strong>    public static function getNextVersionNumber($model, $id) {<\/strong>\r\n\r\n<strong>        return self::where('model', $model)<\/strong>\r\n<strong>                -&gt;where('model_id', $id)<\/strong>\r\n<strong>                -&gt;max('version') + 1;<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<p>\u3053\u306e\u4e2d\u3067\u3084\u3063\u3066\u3044\u308b\u306e\u306f\u3001\u307e\u305a<code>$casts<\/code>\u3092\u4f7f\u3063\u3066\u4ee5\u4e0b\u306e\u81ea\u52d5\u5909\u63db\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u6587\u5b57\u5217 \u2192 JSON<\/li>\n<li>JSON \u2192 \u6587\u5b57\u5217<\/li>\n<\/ul>\n<p>\u305d\u3057\u3066\u3001<code>histories<\/code>\u30c6\u30fc\u30d6\u30eb\u306b\u4fdd\u5b58\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u300c<strong>\u6642\u3092\u623b\u305d\u3046\uff01<\/strong>\u300d\u3092\u5b9f\u884c\u3059\u308b<code>revert()<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001\u5b9f\u969b\u306b\u306f<\/p>\n<pre><strong>$history-&gt;revert();<\/strong><\/pre>\n<p>\u3068\u3044\u3046\u4f7f\u3044\u65b9\u3092\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001\u3053\u306e\u4e2d\u3067\u91cd\u8981\u306a\u306e\u304c<code>Model::withoutEvents()<\/code>\u306e\u90e8\u5206\u3067\u3059\u3002<br \/>\n\u3053\u306e\u4e2d\u3067\u4fdd\u5b58\u3055\u308c\u305f\u3082\u306e\u306f\u3001\u30a4\u30d9\u30f3\u30c8\u304c\u3042\u3063\u3066\u3082\u7121\u8996\u3055\u308c\u308b\u3053\u3068\u306b\u306a\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u3046\u3057\u3066\u3044\u306a\u3044\u3068\u81ea\u52d5\u7684\u306b<code>updated<\/code>\u306e\u30c7\u30fc\u30bf\u304c\u8ffd\u52a0\u3055\u308c\u3066\u3057\u307e\u3046\u305f\u3081\u3067\u3059\u3002\uff08\u5b9f\u969b\u306b\u306f\u4ee3\u308f\u308a\u306b<code>reverted<\/code>\u306e\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3057\u307e\u3059\uff09<\/p>\n<h1>\u30c7\u30fc\u30bf\u64cd\u4f5c\u3092\u3057\u305f\u6642\u306b\u5b9f\u884c\u3055\u308c\u308b\u30a4\u30d9\u30f3\u30c8\u3092\u3064\u304f\u308b<\/h1>\n<p>\u6b21\u306b\u3001\u30c7\u30fc\u30bf\u64cd\u4f5c\u3059\u308b\u305f\u3073\u306b<code>histories<\/code>\u306b\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3059\u308b\u306e\u306f\u3081\u3093\u3069\u3046\u306a\u306e\u3067\u3001\u81ea\u52d5\u7684\u306b\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u308c\u308b\u300c\u30a4\u30d9\u30f3\u30c8\u300d\u3092\u3064\u304f\u3063\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u306a\u304a\u3001\u4eca\u56de\u306f\u4e00\u6c17\u306b\u30a4\u30d9\u30f3\u30c8\u3092\u3064\u304f\u308a\u3084\u3059\u3044<code>Observer<\/code>\u3092\u4f7f\u3063\u305f\u65b9\u6cd5\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>\u5c65\u6b74\u7528\u30a4\u30d9\u30f3\u30c8\u3092\u3064\u304f\u308b<\/h2>\n<p>\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u5c02\u7528<code>Observer<\/code>\u3092\u3064\u304f\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>php artisan make:observer HistoryObserver<\/strong><\/pre>\n<p>\u3059\u308b\u3068\u3001\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u4e2d\u8eab\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/p>\n<p><strong>app\/Observers\/HistoryObserver.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Observers;\r\n\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n\r\nclass HistoryObserver\r\n{\r\n    <strong>public function created(Model $model) {<\/strong>\r\n\r\n<strong>        $this-&gt;saveHistory($model, 'created');<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n\r\n<strong>    public function updated(Model $model) {<\/strong>\r\n\r\n<strong>        $this-&gt;saveHistory($model, 'updated');<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n\r\n<strong>    public function deleted(Model $model) {<\/strong>\r\n\r\n<strong>        $this-&gt;saveHistory($model, 'deleted');<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n\r\n<strong>    private function saveHistory($model, $type) {<\/strong>\r\n\r\n<strong>        $model_class = get_class($model);<\/strong>\r\n<strong>        $primary_key = $model-&gt;getKeyName();<\/strong>\r\n<strong>        $model_id = $model-&gt;{$primary_key};<\/strong>\r\n<strong>        $version = \\App\\History::getNextVersionNumber($model_class, $model_id);<\/strong>\r\n<strong>        $hidden = $model-&gt;getHidden();<\/strong>\r\n<strong>        $data = $model-&gt;makeVisible($hidden);<\/strong>\r\n\r\n<strong>        $history = new \\App\\History;<\/strong>\r\n<strong>        $history-&gt;model = $model_class;<\/strong>\r\n<strong>        $history-&gt;model_id = $model_id;<\/strong>\r\n<strong>        $history-&gt;version = $version;<\/strong>\r\n<strong>        $history-&gt;type = $type;<\/strong>\r\n<strong>        $history-&gt;data = $data;<\/strong>\r\n<strong>        $history-&gt;save();<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<p>\u306a\u304a\u30a4\u30d9\u30f3\u30c8\u3068\u3057\u3066\u5b9f\u884c\u3055\u308c\u308b\u306e\u306f\u4ee5\u4e0b\u306e\uff13\u3064\u3067\u3001<code>saveHistory()<\/code>\u306f\u305d\u308c\u305e\u308c\u306e\u30e1\u30bd\u30c3\u30c9\u304b\u3089\u547c\u3073\u51fa\u3055\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li>created\uff1a \u30c7\u30fc\u30bf\u304c\u4f5c\u6210\u3055\u308c\u305f\u6642<\/li>\n<li>updated\uff1a \u30c7\u30fc\u30bf\u304c\u66f4\u65b0\u3055\u308c\u305f\u6642<\/li>\n<li>deleted\uff1a \u30c7\u30fc\u30bf\u304c\u524a\u9664\u3055\u308c\u305f\u6642<\/li>\n<\/ul>\n<h2>\u4f5c\u6210\u3057\u305f\u30a4\u30d9\u30f3\u30c8\u3092\u6709\u52b9\u306b\u3059\u308b<\/h2>\n<p>\u3067\u306f\u3001\u5148\u307b\u3069\u4f5c\u6210\u3057\u305f<code>HistoryObserver<\/code>\u3092\u6709\u52b9\u306b\u3057\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<br \/>\n\u4eca\u56de\u306f<code>User\u30e2\u30c7\u30eb<\/code>\u3067\u300c<strong>\u6642\u3092\u623b\u305d\u3046\uff01<\/strong>\u300d\u6a5f\u80fd\u3092\u3064\u304b\u3046\u3053\u3068\u306b\u3057\u307e\u3059\u3002<\/p>\n<p><strong>app\/Providers\/AppServiceProvider.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Providers;\r\n\r\n<strong>use App\\Observers\\HistoryObserver; \/\/ \ud83d\udc48 \u8ffd\u52a0<\/strong>\r\nuse Illuminate\\Support\\ServiceProvider;\r\n\r\nclass AppServiceProvider extends ServiceProvider\r\n{\r\n    \/\/ \u7701\u7565\r\n\r\n    public function boot()\r\n    {\r\n        <strong>\\App\\User::observe(HistoryObserver::class); \/\/ \ud83d\udc48 \u8ffd\u52a0<\/strong>\r\n    }\r\n}<\/pre>\n<p>\u203b\u3064\u307e\u308a\u3001\u3082\u3057<code>Item\u30e2\u30c7\u30eb<\/code>\u3067\u4f7f\u3044\u305f\u3044\u5834\u5408\u306f\u3001<code>\\App\\Item::observe(HistoryObserver::class);<\/code>\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3055\u3041\u3001\u3053\u308c\u3067\u6e96\u5099\u306f\u5b8c\u4e86\u3057\u307e\u3057\u305f\u2757<\/p>\n<h1>\u30c6\u30b9\u30c8\u3057\u3066\u307f\u308b<\/h1>\n<p>\u3067\u306f<code>users<\/code>\u3068<code>histories<\/code>\u30c6\u30fc\u30d6\u30eb\u3092\u7a7a\u306b\u3057\u3066\u30c6\u30b9\u30c8\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7649\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_1.png\" alt=\"\" width=\"526\" height=\"256\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_1.png 526w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_1-300x146.png 300w\" sizes=\"auto, (max-width: 526px) 100vw, 526px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7650\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_2.png\" alt=\"\" width=\"524\" height=\"256\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_2.png 524w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_2-300x147.png 300w\" sizes=\"auto, (max-width: 524px) 100vw, 524px\" \/><\/p>\n<p>\u307e\u305a\u306f\u3001\u30e6\u30fc\u30b6\u30fc\u8ffd\u52a0\u3067\u3059\u3002<\/p>\n<pre><strong>$user = new \\App\\User;\r\n$user-&gt;name = '\u3059\u3053\u3072';\r\n$user-&gt;email = 'sukohi.1@example.com';\r\n$user-&gt;password = bcrypt('secret');\r\n$user-&gt;save();<\/strong><\/pre>\n<p>\u3053\u308c\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u30c6\u30fc\u30d6\u30eb\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\uff11\u4ef6\u305a\u3064\u30c7\u30fc\u30bf\u767b\u9332\u3055\u308c\u307e\u3057\u305f\u3002\uff08<code>histories<\/code>\u306e\u30c7\u30fc\u30bf\u306f\u3001<code>created\u30bf\u30a4\u30d7<\/code>\u3068\u3057\u3066\u767b\u9332\u3055\u308c\u3066\u3044\u307e\u3059\uff09<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7651\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_3.png\" alt=\"\" width=\"640\" height=\"270\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_3.png 640w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_3-300x127.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7652\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_4.png\" alt=\"\" width=\"640\" height=\"270\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_4.png 640w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_4-300x127.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>\u7d9a\u3044\u3066\u3001\u3053\u306e\u30e6\u30fc\u30b6\u30fc\u306e\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u3092\u66f4\u65b0\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>$user = \\App\\User::find(1);\r\n$user-&gt;email = 'sukohi.2@example.com';\r\n$user-&gt;save();<\/strong><\/pre>\n<p>\u3053\u308c\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u30e6\u30fc\u30b6\u30fc\u30c7\u30fc\u30bf\u304c\u66f4\u65b0\u3055\u308c\u3001<code>histories<\/code>\u306b<code>updated<\/code>\u306e\u65b0\u3057\u3044\u30c7\u30fc\u30bf\u304c\u8ffd\u52a0\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7653\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_5.png\" alt=\"\" width=\"640\" height=\"270\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_5.png 640w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_5-300x127.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7654\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_6.png\" alt=\"\" width=\"640\" height=\"300\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_6.png 640w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_6-300x141.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>\u6b21\u306b\u3001\u3053\u306e\u30e6\u30fc\u30b6\u30fc\u3092\u524a\u9664\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>$user = \\App\\User::find(1);\r\n$user-&gt;delete();<\/strong><\/pre>\n<p>\u3053\u308c\u3092\u5b9f\u884c\u3059\u308b\u3068\u30e6\u30fc\u30b6\u30fc\u30c6\u30fc\u30d6\u30eb\u306f\u7a7a\u306b\u306a\u308a\u3001\u3055\u3089\u306b<code>histories<\/code>\u306b<code>deleted<\/code>\u306e\u30c7\u30fc\u30bf\u304c\u8ffd\u52a0\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7649\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_1.png\" alt=\"\" width=\"526\" height=\"256\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_1.png 526w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_1-300x146.png 300w\" sizes=\"auto, (max-width: 526px) 100vw, 526px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7655\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_7.png\" alt=\"\" width=\"640\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_7.png 640w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_7-300x150.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>\u3067\u306f\u6700\u5f8c\u306b\u3001\u524a\u9664\u3057\u305f\u30c7\u30fc\u30bf\u3092<code>histories\u30c6\u30fc\u30d6\u30eb<\/code>\u306e<code>ID:\uff13<\/code>\uff08\u3064\u307e\u308a\u524a\u9664\u76f4\u524d\uff09\u306b\u623b\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>$history = \\App\\History::find(3);\r\n$history-&gt;revert();<\/strong><\/pre>\n<p>\u5b9f\u884c\u5f8c\u306f\u3053\u3046\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7653\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_5.png\" alt=\"\" width=\"640\" height=\"270\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_5.png 640w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_5-300x127.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7656\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_8.png\" alt=\"\" width=\"640\" height=\"340\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_8.png 640w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/08\/laravel_revert_8-300x159.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>\u306f\u3044\u2757\u524a\u9664\u3057\u305f\u30c7\u30fc\u30bf\u304c\u5143\u3082\u623b\u308a\u3001\u3055\u3089\u306b\u5143\u306b\u623b\u3057\u305f\u3053\u3068\u304c\u308f\u304b\u308b<code>reverted<\/code>\u306e\u30c7\u30fc\u30bf\u304c<code>histories<\/code>\u306b\u8ffd\u52a0\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u6210\u529f\u3067\u3059\ud83d\ude0a\u2728<\/p>\n<h1>\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b<\/h1>\n<p>\u4eca\u56de\u5b9f\u969b\u306b\u958b\u767a\u3057\u305f\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u4e00\u5f0f\u3092\u4ee5\u4e0b\u304b\u3089\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<a  data-e-Disable-Page-Transition=\"true\" class=\"download-link\" title=\"\" href=\"https:\/\/blog.capilano-fw.com?download=7670&amp;tmstv=1777532672\" rel=\"nofollow\" id=\"download-link-7670\" data-redirect=\"false\" >\n\t\u3010Laravel\u3011DB\u64cd\u4f5c\u306erevert\uff08\u6642\u3092\u623b\u305d\u3046\uff09\u6a5f\u80fd\u3092\u3064\u304f\u308b<\/a>\n\n<p>\u203b\u305f\u3060\u3057\u3001users\u30c6\u30fc\u30d6\u30eb\u306a\u3069\u306f\u3054\u81ea\u8eab\u3067\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u4eca\u56de\u306f\u3068\u3066\u3082\u30b7\u30f3\u30d7\u30eb\u306a\u69cb\u6210\u3067\u5b9f\u88c5\u3092\u3057\u3066\u307f\u307e\u3057\u305f\u304c\u3001\uff11\u70b9\u3060\u3051\u6c17\u306b\u306a\u308b\u306e\u304c\u3001\u300c<strong>\u30c7\u30fc\u30bf\u91cf\u306e\u591a\u3055<\/strong>\u300d\u3067\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001\u305f\u304f\u3055\u3093\u306e\u30c7\u30fc\u30bf\u3092\u4f55\u5ea6\u3082\u66f8\u304d\u63db\u3048\u308b\u5fc5\u8981\u304c\u3042\u308b\u30b7\u30b9\u30c6\u30e0\u3067\u306f\u30c7\u30fc\u30bf\u91cf\u304c\u591a\u304f\u306a\u3063\u3066\u3057\u307e\u3046\u53ef\u80fd\u6027\u304c\u3042\u308b\u306e\u3067\u3001\u305d\u3046\u3044\u3063\u305f\u5834\u5408\u306f<strong>\u91cd\u8907\u3059\u308b\u90e8\u5206\u3092\u7701\u7565\u3067\u304d\u308b\u3088\u3046\u306b\u300c\u4f55\u306e\u3069\u3053\u3092\u5909\u66f4\u3057\u305f\u304b\u300d\u3068\u3044\u3046\u3088\u3046\u306a\u7701\u30a8\u30cd\u8a2d\u8a08\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b<\/strong>\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u305f\u3060\u3001\u305d\u3046\u306a\u308b\u3068\u8907\u96d1\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u4eca\u5f8c\u6642\u9593\uff06\u30a2\u30a4\u30c7\u30a2\u304c\u601d\u3044\u6d6e\u304b\u3093\u3060\u3089\u6311\u6226\u3057\u3066\u307f\u307e\u3059\ud83d\udc4d\uff08<code>Dropbox<\/code>\u3068\u304b<code>git<\/code>\u3068\u304b\u306f\u305d\u3093\u306a\u611f\u3058\u306a\u306e\u304b\u306a\uff1f\uff1f\uff09<\/p>\n<p>\u3068\u3082\u3042\u308c\u3001\u305c\u3072\u7686\u3055\u3093\u3082\u8a66\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u306d\u3002<\/p>\n<p>\u3067\u306f\u3067\u306f\u301c\uff01<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6067 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/03\/no_19.png\" alt=\"\" width=\"314\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/03\/no_19.png 314w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/03\/no_19-294x300.png 294w\" sizes=\"auto, (max-width: 314px) 100vw, 314px\" \/><br \/>\n\u300c\u30c1\u30fc\u725b\u3063\u3066\u305d\u3093\u306a\u610f\u5473\u306a\u306e\u2757\u2753\ud83d\ude02\u300d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u3053\u308c\u306f\u79c1\u306b\u9650\u3089\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u4f55\u6c17\u306a\u304f\u904e\u3054\u3057\u3066\u3044\u308b\u3068\u300c\u305d\u3046\u3044\u3048\u3070\u30a2\u30ec\u9762\u767d\u304b\u3063\u305f\u306a\u301c\u300d\u3068\u30af\u30b9\u3063\u3068\u3057\u3066\u3057\u307e\u3063\u305f\u308a\u3057\u307e\u3059\u3002 \u305d\u3057\u3066\u5148\u65e5\u306f\u305d\u306e\u5f71\u97ff\u3067La &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=7646\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;\u3010Laravel\u3011DB\u64cd\u4f5c\u306erevert\uff08\u6642\u3092\u623b\u305d\u3046\uff09\u6a5f\u80fd\u3092\u3064\u304f\u308b&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":7667,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,97],"tags":[60],"class_list":["post-7646","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","category-97","tag-60"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/7646","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7646"}],"version-history":[{"count":13,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/7646\/revisions"}],"predecessor-version":[{"id":7669,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/7646\/revisions\/7669"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/7667"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}