Adding Soft Deletes to Existing Database Table in Laravel

First, enable soft deletes in your model. I’ll use the users table as an example.

use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model {

    use SoftDeletes;

    protected $dates = ['deleted_at'];

}

Then, create a new migration to modify the existing table.

/php artisan make:migration add_soft_deletes_to_user_table --table="users"

Open up the newly created migration file and add the softDeletes method.

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddSoftDeletesToUserTable extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::table('users', function(Blueprint $table)
		{
			$table->softDeletes();
		});
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::table('users', function(Blueprint $table)
		{
            $table->dropSoftDeletes();
		});
	}

}

Now, run your migration.

php artisan migrate

You should now see the deleted_at timestamp column in your database.

Leave a Reply

Your email address will not be published. Required fields are marked *