Soft Deletes
To make a model soft-deletable, just issue the softDeletes
or softDeletesTz
into the columns list. Larawiz will automatically detect and use the SoftDeletes
trait for the Model.
yaml
models:
Post:
title: string
softDeletes: ~
1
2
3
4
2
3
4
php
class Post extends Model
{
use SoftDeletes;
// ...
}
1
2
3
4
5
6
2
3
4
5
6
php
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->softDeletes();
$table->timestamps();
});
1
2
3
4
5
6
2
3
4
5
6
You can also issue the column name to use as soft-deletes, that will be reflected in the model itself.
yaml
models:
Post:
title: string
softDeletes: removed_at
1
2
3
4
2
3
4
php
class Podcast extends Model
{
use SoftDeletes;
protected const DELETED_AT = 'removed_at';
}
1
2
3
4
5
6
2
3
4
5
6
php
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->softDeletes('removed_at');
$table->timestamps();
});
1
2
3
4
5
6
2
3
4
5
6
Deleted factory state
When you create a model with soft deletes and factories enabled by default, Larawiz will conveniently create a trashed
factory state, for free.
yaml
models:
Post:
title: string
softDeletes: soft_deleted_at
1
2
3
4
2
3
4
php
class PostFactory extends Factory
{
public function definition()
{
// ...
}
public function trashed()
{
return $this->state([
$this->newModel()->getDeletedAtColumn() => now(),
]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14