Implementation of Soft Delete In Laravel 5 it’s a quite different from previous versions.
Instructions:
Add SoftDeletes method to your your migration files:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateTable extends Migration { public function up() { Schema::create('new_table', function(Blueprint $table) { $table->softDeletes(); }); } } |
In your model use the SoftDelete trait as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class MyModel extends Base { use SoftDeletes; /** * The database table used by the model. * * @var string */ protected $table = 'my_table'; } |
Finally, you are now able to delete a model using SoftDelete:
1 2 |
$model = MyModel::find(1); $model->delete(); |