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 1 year has passed since last update.

Laravel 論理削除したデータの復元

Posted at

tinkerを使用して復元

  • まずは論理削除されたデータの取得
php artisan tinker
Psy Shell v0.10.3 (PHP 7.3.33  cli) by Justin Hileman
>>> $deleteItem = App\Http\Models\User::onlyTrashed()->get();

うまく取得できれば下記のように一覧が表示される
=> Illuminate\Database\Eloquent\Collection {#3241
     all: [
       App\Http\Models\User {#3258
         id: 3,
         name: "editor",
         email: "editor@example.com",
         created_at: "2022-12-16 06:38:12",
         updated_at: "2022-12-28 05:47:41",
         deleted_at: "2022-12-28 05:47:41",
       },
       App\Http\Models\User {#3276
         id: 4,
         name: "テスト太郎",
         email: "test@example.com",
         created_at: "2022-12-22 00:57:04",
         updated_at: "2022-12-28 05:45:57",
         deleted_at: "2022-12-28 05:45:57",
       },
       
  • 取得したデータから所定のIDで復元
>>> $deleteItem->find(3)->restore();
=> true

成功すればtrueが表示される。
上記の場合にはid: 3のユーザーが復元されたことになる。

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?