I've still no solution for this, and I'd really appreciate help in any form. Is there some clever way I can debug this? Is there some more in-depth documentation that I've missed on soft delete somewhere?
Jul 17, 2018
3
Level 1
Soft delete modifies custom date field in database table
I have a table with a field 'expires_at'. When soft deleting an entry and all it's children, it has it's deleted_at and updated_at field change. But for some reason, it also changes my custom field expires_at to the same value. Is this the expected behaviour, or am I doing something wrong?
Migrations:
Schema::create( 'licensegroups', function ( Blueprint $table ) {
$table->increments( 'id' );
$table->string( 'name' );
$table->timestamp( 'expires_at' );
$table->timestamps();
$table->softDeletes();
} );
Schema::create( 'licenses', function ( Blueprint $table ) {
$table->increments( 'id' );
$table->integer( 'licensegroup_id' )->unsigned();;
$table->integer( 'quantity' )->unsigned();;
$table->string( 'key' );
$table->timestamps();
$table->softDeletes();
$table->foreign('licensegroup_id')->references('id')->on('licensegroups')->onDelete('cascade');
} );
Models:
class Licensegroup extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
// ...
public function licenses()
{
return $this->hasMany( License::class );
}
public static function boot()
{
parent::boot();
static::deleting(function($licensegroup)
{
$licensegroup->licenses()->delete();
});
static::restoring(function($licensegroup)
{
$licensegroup->licenses()->restore();
});
}
}
class License extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
// ...
}
I then use $licensegroup->delete(); to delete the parent with it's children.
Level 21
Change
$table->timestamp( 'expires_at' );
to
$table->datetime( 'expires_at' );
1 like
Please or to participate in this conversation.