Laravel 소프트 삭제

1 개요[ | ]

라라벨 soft deleting
Laravel 소프트 삭제, 약한 삭제
  • 라라벨 Eloquent 는 soft delete를 지원한다.
  • soft delete 설정한 후 데이터를 삭제하면 겉으로는 삭제 되나 실제 DB에는 데이터가 남아 있다.
  • 라라벨에서 soft delete 설정 후 삭제하면 deleted_at 컬럼에 시간이 남게 된다.
자료형은 timestamp, 기본값은 null이다.

2 설정 방법[ | ]

Model 파일 추가 내용
  • use Illuminate\Database\Eloquent\SoftDeletes; 추가
  • use SoftDeletes; 추가
  • protected $dates = ['deleted_at']; 추가
<?php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Flight extends Model
{
    use SoftDeletes;
    protected $dates = ['deleted_at'];
}
Migration 파일 추가 내용
  • $table->SoftDeletes() 항목 추가
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class Flights extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->increments('id');
            // 생략
            $table->SoftDeletes();
            // 생략
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('flights');
    }
}

3 같이보기[ | ]

4 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}