Innanzitutto, abilita le Soft Deletes nel model. Useremo la tabella degli utenti come esempio.
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model {
use SoftDeletes;
protected $dates = ['deleted_at'];
}
Quindi, crea una nuova migration per modificare la tabella esistente.
php artisan make:migration add_soft_deletes_to_user_table --table="users"
Apri il file di migration appena creato e aggiungi il metodo softDeletes.
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
return new class 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();
});
}
}
Ora esegui la migrazione.
php artisan migrate
Ora dovresti vedere la colonna timestamp delete_at nel tuo database.
Maggiori informazioni le trovate nella documentazione ufficiale
https://laravel.com/docs/master/eloquent#soft-deleting