Installation Add the package via Composer:
composer require axstrad/doctrine-extensions
Register the service provider in config/app.php under providers:
Axstrad\DoctrineExtensions\DoctrineExtensionsServiceProvider::class,
Basic Setup
Ensure Doctrine ORM is properly configured in config/database.php (Laravel's default setup usually suffices). The package extends Doctrine's behavior, so no additional config is needed unless using specific extensions.
First Use Case: Soft Deletes Implement soft deletes in an Eloquent model:
use Axstrad\DoctrineExtensions\SoftDeleteable\SoftDeleteableTrait;
class User extends Model
{
use SoftDeleteableTrait;
protected $dates = ['deleted_at'];
}
Now, User::destroy() will set deleted_at instead of deleting records.
Soft Deletes
withTrashed() to include soft-deleted records:
User::withTrashed()->get();
forceDelete() to permanently remove records:
$user->forceDelete();
Sluggable Fields
getSlug() method in your model:
class Post extends Model
{
public function getSlug()
{
return Str::slug($this->title);
}
}
sluggable behavior in queries or migrations.Tree Structures (Nested Sets)
NestedSetTrait:
class Category extends Model
{
use NestedSetTrait;
protected $parentColumn = 'parent_id';
}
getChildren(), getAncestors(), and moveUp() are available.Timestamps
created_at/updated_at with custom logic (e.g., auto-updating on specific fields):
use Axstrad\DoctrineExtensions\Timestampable\TimestampableTrait;
class Product extends Model
{
use TimestampableTrait;
protected $timestampable = ['updated_at'];
}
SoftDeleteable in migrations to add deleted_at columns:
Schema::table('users', function (Blueprint $table) {
$table->softDeletes();
});
withTrashed() in admin panels to show deleted records for recovery.Doctrine vs. Eloquent Conflicts
SoftDeleteable) may require manual trait application in Eloquent models.Missing Configuration
Sluggable) require additional setup in Doctrine configurations. Check the docs for extension-specific requirements.php artisan vendor:publish to publish config files if available.Performance with Large Trees
NestedSetTrait can slow queries on deep hierarchies. Use MaterializedPathTrait for better performance in large trees.lft/rgt columns in migrations:
$table->integer('lft')->index();
$table->integer('rgt')->index();
Soft Deletes and Relationships
withTrashed() on both sides of relationships:
User::with(['posts' => function ($query) {
$query->withTrashed();
}])->withTrashed()->get();
Query Logs: Enable Doctrine query logging in config/database.php:
'logging' => true,
Check logs for malformed queries (e.g., missing deleted_at in WHERE clauses).
Trait Conflicts: If traits behave unexpectedly, verify:
boot() in Laravel models).Custom Slug Logic
Override getSlug() or use a callback:
use Axstrad\DoctrineExtensions\Sluggable\SluggableTrait;
class Post extends Model
{
use SluggableTrait;
public function getSlugOptions()
{
return ['source' => 'custom_title'];
}
}
Soft Delete Events
Listen for deleting and deleted events to add custom logic:
User::deleting(function ($user) {
// Pre-delete logic
});
User::deleted(function ($user) {
// Post-delete logic (e.g., log deletion)
});
Timestampable Extensions
Extend TimestampableTrait to add custom timestamp fields:
protected $timestampable = [
'created_at',
'updated_at',
'archived_at' => 'archive', // Custom field
];
How can I help you explore Laravel packages today?