Updated 29 June 2023
Hello Everyone, In this Article We will learn about Soft Deletes feature of Laravel.
Soft Deleting in Laravel permits you to mark a database record as “deleted” instead of actually deleting it from the database. When records are soft deleted, they are not actually removed from your database. A “deleted_at” attribute is set on the record indicating the date and time at which the record was “deleted”. So that if we incorrectly deleted the specific record we can easily restore them. That’s why Soft Deletes feature is essential and need to exist in our Laravel application.
To enable Soft Deletes for your Laravel application follow the steps written below:
In this step we will add “deleted_at” column to our table. We can create this column with softDeletes() helper method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name')->nullable(); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->softDeletes(); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } } |
In this step we have to add Illuminate\Database\Eloquent\SoftDeletes trait to the model.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?php namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Database\Eloquent\SoftDeletes; use Laravel\Sanctum\HasApiTokens; class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable, SoftDeletes; /** * The attributes that are mass assignable. * * @var array<int, string> */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for serialization. * * @var array<int, string> */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast. * * @var array<string, string> */ protected $casts = [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; /** * The attributes that should be mutated to dates. * * @var array */ protected $dates = ['deleted_at']; } |
In this step we will learn about Queries of Soft deletes and Restore Deleted Records.
Delete Query:
1 2 |
$user = User::find(1); $user->delete(); |
Restore Query:
1 |
$user = User::onlyTrashed()->restore(); |
Get Deleted Records:
1 |
$user = User::withTrashed()->get(); |
I hope you will like the Tutorial and it will help you to learn about Laravel Soft Deletes. Please comment below if you have any questions.
You can also hire laravel developers to build your custom solutions on laravel. For exploring the available extensions for Bagisto, you can check out the bagisto extension marketplace.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.