Category: Laravel

It's common that a user in your applications makes mistakes and deletes data by accident – even if you show them countless warnings before they delete the record from your database. In other scenarios you can introduce the concept of a trash that your users know from their desktop and delete trashed records after a while.

Add soft delete to the User model with a new column in the up() method of a new migration: Schema::table('users', function (Blueprint $table) { $table->softDeletes(); }); Add the SoftDeletes trait to the User model: class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable, SoftDeletes; //... } From now on, your application doesn't delete Users from the database anymore when you run the delete method on a user but sets a date when this user got deleted.

User::first()->forceDelete(); If you want to clean up your database regularly and delete soft deleted models automatically, this is called https://laravel.com/docs/8.x/eloquent#pruning-models.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners