HasOwner for single-type ownership, HasMorphOwner for polymorphic) that align with common Laravel use cases (e.g., user-owned resources vs. multi-entity ownership like teams, organizations, or admins).Ownable contracts from implementations (Cog\Contracts\Ownership\) enables loose coupling, making it easier to swap or extend ownership logic without modifying core models.Auth::user()) via OwnableObserver reduces boilerplate in model creating events, though this may require careful handling in edge cases (e.g., API vs. web contexts).ownedBy(), notOwnedBy()) integrate seamlessly with Laravel’s query builder, reducing custom logic for ownership filtering.use HasOwner or use HasMorphOwner) and implementing CanBeOwner (if polymorphic) is straightforward. Existing belongsTo/morphTo relationships can often be replaced with the package’s traits.owned_by_id (and owned_by_type for polymorphic), which is a minor addition if not already present. Migrations or schema updates may be needed for backward compatibility.Auth facade for default owner resolution, but custom logic (e.g., resolving owners from context) is supported via resolveDefaultOwner().HasMorphOwner bypasses type validation, which could lead to runtime errors if owner types are mismanaged (e.g., assigning a Team to a model expecting a User). Strict mode (HasOwner) mitigates this but limits flexibility.OwnableObserver may conflict with explicit owner-setting logic or API use cases where ownership is determined externally (e.g., via API payloads). Disabling the observer globally or per-model requires configuration.morphTo) may introduce slight overhead due to additional joins or type checks. Benchmarking is recommended for high-traffic models.owned_by_id for legacy records).user_id)? How will these be mapped to owned_by_id without data loss?morphTo been benchmarked?OwnableObserver be disabled for specific models or contexts?CanBeOwner types in polymorphic mode) be logged or surfaced to users?Ownable contract be extended to support additional ownership attributes (e.g., shared_with, transfer_history)?Auth), and service providers. It integrates cleanly with Laravel’s dependency injection and event systems.user_id, team_id). Document conflicts or gaps.HasOwner (strict) vs. HasMorphOwner (polymorphic).owned_by_id (and owned_by_type for polymorphic) to target models via migrations. Use Laravel’s schema builder for consistency:
Schema::table('posts', function (Blueprint $table) {
$table->foreignId('owned_by_id')->constrained()->nullOnDelete();
$table->string('owned_by_type')->nullable(); // For polymorphic
});
owned_by_id from existing owner fields or mark records as abandoned.// Strict ownership (e.g., only User)
use Cog\Laravel\Ownership\Traits\HasOwner;
class Post extends Model {
use HasOwner;
protected $ownerType = User::class;
}
// Polymorphic ownership (e.g., User or Team)
use Cog\Laravel\Ownership\Traits\HasMorphOwner;
class Asset extends Model {
use HasMorphOwner;
}
CanBeOwner for polymorphic owner types (e.g., User, Team).OwnershipServiceProvider::ignoreObserver();
resolveDefaultOwner() for custom logic (e.g., resolving from context):
class Post extends Model {
use HasOwner;
public function resolveDefaultOwner(): ?User {
return Auth::guard('api')->user(); // Custom context
}
}
// Before: Post::where('user_id', $user->id)
// After: Post::ownedBy($user)
changeOwnerTo(), abandonOwner()).belongsTo/morphTo relationships may require updates to queries or serializers (e.g., JSON:API resources).spatie/laravel-permission).Cog\Contracts\Ownership\ interfaces to isolate changes from implementations.resolveDefaultOwner() or observer behavior should be documented and tested.Ownable contract (e.g., new methods) may require updates to all using models.How can I help you explore Laravel packages today?