Mengenal apa itu soft delete di laravel
Selamat pagi, siang, sore atau malam sobs-sobs sekalian, semoga sobs-sobs masih diberikan kesehatan sehingga bisa membaca artikel ini dengan ditemani kopi hitam panas dan seutas gorengan.
Oke pada pembahasan kali ini kita akan membahas tentang si framework tamvan yaitu Laravel dengan fokus pada fiturnya yaitu soft delete.
Bagi yang sudah familiar dengan materi ini boleh di skip terlebih dahulu, dan bagi yang belum mari kita mulai dengan apa yang dimaksud soft delete di laravel,
Soft delete adalah sebuah mekanisme menghapus data secara temporary atau sementara, biasanya jika sobs-sobs sekalian menghapus data dilaravel maka data tersebut sepenuhnya hilang benar-benar tidak ada, maka dengan soft delete ini sebenarnya data tidak dihapus hanya saja akan dihide atau disembunyikan jika kita melakukan query konsep seperti ini sama seperti recycle bin. hehehe sorry ya kalo analoginya sedikit ngaco.
Sebenarnya prinsip kerja cukup sederhana jika sebelumnya dilaravel kita membuat file migrasi secara default maka akan ada schema $table->timestamps dimana itu digunakan untuk membuat kolom didatabase yaitu created_at dan updated_at, nah jika kita ingin suatu resource kita menggunakan soft delete maka kolom deleted_at akan kita buat juga.
Oke dari pada bingung disini gua akan buat contohnya dengan membuat Model Employee beserta file migrasinya :
php artisan make:model Employee --migration --seed
Jika sudah silahkan sobs-sobs buka migration filenya terlebih dahulu,
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('employees', function (Blueprint $table) {
$table->id();
$table->string('name', 100);
$table->string('email', 100)->unique();
$table->timestamps();
// cara pertama
// $table->timestamp('deleted_at')->nullable();
// cara kedua (rekomendasi)
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('employees');
}
};
Perhatikan sobs-sobs
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Employee extends Model
{
use HasFactory;
// implementasi trait soft delete
use SoftDeletes;
protected $guarded = ['id'];
}
trait soft delete ini berada di Illuminate\Database\Eloquent\SoftDeletes lalu kita implementasikannya dengan mengunakan keyword use.
<?php
namespace Database\Seeders;
use App\Models\Employee;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class EmployeeSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
Employee::create([
'name' => 'sruputkode',
'email' => 'sruputkode@outlook.co.id',
]);
Employee::create([
'name' => 'kirana',
'email' => 'kirana@outlook.co.id',
]);
}
}
diatas gua buat dua contoh data employee sobs-sobs, selanjutnya untuk pengetesan gua akan buat sebuah unit test, lah bang kok testnya engga lewat controller ?
php artisan make:test SoftDeleteTest
Selanjutnya kita buka file unit testnya yang berada di test/Feature/SoftDeleteTest.php
<?php
namespace Tests\Feature;
use App\Models\Employee;
use Database\Seeders\EmployeeSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class SoftDeleteTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
DB::delete('DELETE FROM employees');
}
// unit test softDelete
public function testDelete()
{
// seed
$this->seed(EmployeeSeeder::class);
// delete
$sruputkode = Employee::where('name', 'sruputkode')->first();
$sruputkode->delete();
// ambil semua data employee
$employees = Employee::all();
self::assertCount(1, $employees);
}
}
<?php
namespace Tests\Feature;
use App\Models\Employee;
use Database\Seeders\EmployeeSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class SoftDeleteTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
DB::delete('DELETE FROM employees');
}
// unit test softDelete
public function testDelete()
{
// seed
$this->seed(EmployeeSeeder::class);
// delete
$sruputkode = Employee::where('name', 'sruputkode')->first();
$sruputkode->delete();
// ambil semua data employee
$employees = Employee::all();
self::assertCount(1, $employees);
}
// unit test forceDelete (Hapus data permanen)
public function testForceDelete()
{
// seed
$this->seed(EmployeeSeeder::class);
// force delete (delete permanen)
$sruputkode = Employee::where('name', 'sruputkode')->first();
$sruputkode->forceDelete();
// ambil semua data employee
$employees = Employee::all();
self::assertCount(1, $employees);
}
}
perhatikan method testForceDelete, dan sobs-sobs silahkan buka PHPMyAdminnya
<?php
namespace Tests\Feature;
use App\Models\Employee;
use Database\Seeders\EmployeeSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class SoftDeleteTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
DB::delete('DELETE FROM employees');
}
// unit test softDelete
public function testDelete()
{
// seed
$this->seed(EmployeeSeeder::class);
// delete
$sruputkode = Employee::where('name', 'sruputkode')->first();
$sruputkode->delete();
// ambil semua data employee
$employees = Employee::all();
self::assertCount(1, $employees);
}
// unit test forceDelete (Hapus data permanen)
public function testForceDelete()
{
// seed
$this->seed(EmployeeSeeder::class);
// force delete (delete permanen)
$sruputkode = Employee::where('name', 'sruputkode')->first();
$sruputkode->forceDelete();
// ambil semua data employee
$employees = Employee::all();
self::assertCount(1, $employees);
}
// unit test withTrashed (Ambil semua data)
public function testWithTrashed()
{
// seed
$this->seed(EmployeeSeeder::class);
// delete
$sruputkode = Employee::where('name', 'sruputkode')->first();
$sruputkode->delete();
// query semua data termasuk sudah dihapus (softdelete)
$employees = Employee::withTrashed()->get();
self::assertCount(2, $employees);
}
}
Selanjutnya bagaimana bang kalo ingin ambil data yang sudah dihapus saja / softdelete ?
<?php
namespace Tests\Feature;
use App\Models\Employee;
use Database\Seeders\EmployeeSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class SoftDeleteTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
DB::delete('DELETE FROM employees');
}
// unit test softDelete
public function testDelete()
{
// seed
$this->seed(EmployeeSeeder::class);
// delete
$sruputkode = Employee::where('name', 'sruputkode')->first();
$sruputkode->delete();
// ambil semua data employee
$employees = Employee::all();
self::assertCount(1, $employees);
}
// unit test forceDelete (Hapus data permanen)
public function testForceDelete()
{
// seed
$this->seed(EmployeeSeeder::class);
// force delete (delete permanen)
$sruputkode = Employee::where('name', 'sruputkode')->first();
$sruputkode->forceDelete();
// ambil semua data employee
$employees = Employee::all();
self::assertCount(1, $employees);
}
// unit test withTrashed (Ambil semua data)
public function testWithTrashed()
{
// seed
$this->seed(EmployeeSeeder::class);
// delete
$sruputkode = Employee::where('name', 'sruputkode')->first();
$sruputkode->delete();
// query semua data termasuk sudah dihapus (softdelete)
$employees = Employee::withTrashed()->get();
self::assertCount(2, $employees);
}
// unit test onlyTrashed (Ambil semua data hanya softdelete)
public function testOnlyTrashed()
{
// seed
$this->seed(EmployeeSeeder::class);
// delete
$sruputkode = Employee::where('name', 'sruputkode')->first();
$sruputkode->delete();
// query semua data yang hanya dihapus (softdelete)
$employees = Employee::onlyTrashed()->get();
dump($employees);
self::assertCount(1, $employees);
}
}
Kesimpulan :
Posting Komentar untuk "Mengenal apa itu soft delete di laravel"
Posting Komentar