bespoke-support/created-updated-deleted-bundle
Since this is a Symfony bundle, Laravel integration requires Bridge or manual adaptation. For Laravel 5.5+ (Symfony 3.4+ compatible):
Install via Composer (adjust for Laravel):
composer require bespoke-support/created-updated-deleted-bundle
Register the Bundle (Laravel 5.5+):
In config/app.php, add to providers:
BespokeSupport\CreatedUpdatedDeletedBundle\CreatedUpdatedDeletedBundle::class,
Configure Database Events (Laravel-specific):
Override boot() in a ServiceProvider to hook into Eloquent events:
use BespokeSupport\CreatedUpdatedDeletedBundle\EventListener\TimestampListener;
public function boot()
{
$this->app->make(TimestampListener::class)->subscribe();
}
Apply to a Model: Extend your Eloquent model:
use BespokeSupport\CreatedUpdatedDeletedBundle\Traits\Timestampable;
class Post extends Model
{
use Timestampable;
}
Run Migrations: Add columns to your table:
Schema::table('posts', function (Blueprint $table) {
$table->timestampsTz(); // For created_at/updated_at
$table->timestamp('deleted_at')->nullable();
});
Enable soft deletes in Post model:
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use Timestampable, SoftDeletes;
protected $dates = ['deleted_at'];
}
Now Post::where('deleted_at', null) filters active records.
timestamps() and updated_at logic.deleted_at for soft deletes (compatible with Laravel’s SoftDeletes trait).getCreatedAtColumn(), getUpdatedAtColumn(), or getDeletedAtColumn() in your model.Leverage Laravel events to extend functionality:
// In a ServiceProvider
Event::listen('eloquent.saving: Post', function ($model) {
// Custom logic before save
});
Event::listen('eloquent.deleting: Post', function ($model) {
$model->deleted_at = now(); // Force soft delete
});
Return timestamps in API responses:
// PostResource.php
public function toArray($request)
{
return [
'id' => $this->id,
'created_at' => $this->created_at->toDateTimeString(),
'updated_at' => $this->updated_at->toDateTimeString(),
'deleted_at' => $this->deleted_at ? $this->deleted_at->toDateTimeString() : null,
];
}
Add reusable scopes:
class Post extends Model
{
public function scopeActive($query)
{
return $query->whereNull('deleted_at');
}
public function scopeCreatedSince($query, $since)
{
return $query->where('created_at', '>=', $since);
}
}
Usage:
Post::active()->createdSince(now()->subDays(7))->get();
Handle bulk updates/deletes with timestamps:
// Soft delete multiple records
Post::where('user_id', 1)->update(['deleted_at' => now()]);
// Restore deleted records
Post::whereNotNull('deleted_at')->update(['deleted_at' => null]);
TimestampListener to Eloquent events (as shown in Getting Started).timestampsTz() in migrations for timezone-aware timestamps (Laravel 5.6+).touch() or save() overrides block automatic updates.$dates property includes deleted_at:
protected $dates = ['deleted_at'];
created_at, updated_at, and deleted_at:
Schema::table('posts', function (Blueprint $table) {
$table->index('created_at');
$table->index('updated_at');
$table->index('deleted_at');
});
with() for eager loading:
Post::with('author')->active()->get();
setCreatedAt(), setUpdatedAt(), or setDeletedAt() in your model.laravel-auditlog to log changes to these fields:
use Owlish\AuditLog\AuditLoggable;
class Post extends Model
{
use Timestampable, AuditLoggable;
}
php artisan migrate --path=/path/to/migrations
deleted_at, ensure nullable() is set to avoid constraint errors.$factory->define(Post::class, function (Faker $faker) {
return [
'created_at' => $faker->dateTimeThisYear,
'updated_at' => $faker->dateTimeThisMonth,
'deleted_at' => null,
];
});
$post = Post::create([...]);
$post->delete(); // Soft delete
$this->assertSoftDeleted($post);
$this->assertNull(Post::where('id', $post->id)->first());
How can I help you explore Laravel packages today?