

One of the annoying parts about the soft delete trait is that if a user attempts to access a page for a deleted resource they’ll receive a 404 error and not something helpful like a message informing them the item was deleted. Before getting started, be sure to configure a database connection in your application's config/database.php configuration file.

class CreateProjectsTable extends Migration Viewing Trashed Models From a Controller In addition to retrieving records from the database table, Eloquent models allow you to insert, update, and delete records from the table as well.

$table -> string ( 'name' ) $table -> softDeletes () When we open the newly created migration we’ll add the following lines to our up() function. $ php artisan make:model -m ProjectĬreated Migration: 2020_05_02_012758_create_projects_table Likewise, if a map operation returns a collection that does not contain any Eloquent models, it will be converted to a base collection instance. We’ll create the model and migration in one step so we can be as lazy as possible. While most Eloquent collection methods return a new instance of an Eloquent collection, the collapse, flatten, flip, keys, pluck, and zip methods return a base collection instance. Let’s start by creating a new model to track a Project in a project management application. Adding the Soft Delete Columns to a New Table The first part of the process we need to tackle is setting up our database tables to have the SoftDeletes column. Laravel provides support for soft deleting using the Illuminate\Database\Eloquent\SoftDeletes trait. Soft deleting the data allows us to easily view and restore the data with minimal work and can be a huge time saver when data is accidentally deleted.
Eloquent delete full#
Why Should We Use Soft Deletes?Īs we’ve discussed in “Stop Deleting Data”, when we DELETE a row from the database it’s gone forever without going through a potentially painful process of doing a full database restore.
Eloquent delete how to#
This article/video discusses how to use Soft Deletes in Laravel. Thankfully, Laravel provides a built-in feature that allows us to flag database rows as deleted without actually deleting them from the database. If we need the data back, our only solution will be to restore a backup and hope we have the backup just before it was deleted so the loss is minimized. We can’t even look at the data to see if we needed it because it’s gone. The annoying thing about deleting data from a database is that it’s gone forever.
