Back to Notes
LaravelPHPPackageOwnershipEloquent
Laravel Ownership Package – Simplify Model Ownership in Laravel
05/09/2025
Laravel Ownership Package – Simplify Model Ownership in Laravel
Managing ownership relationships between models can get tricky in Laravel projects. The Laravel Ownership package by Dibakar Mitra makes it simple and elegant.
🚀 What is Laravel Ownership?
This package allows you to assign and manage ownership between different models. For example, a User can own a Project, Team, or any other entity. It uses a clean polymorphic approach without repeating boilerplate code.
✨ Features
- Polymorphic ownership handling out of the box.
- Easy to integrate with existing models.
- Supports roles for more advanced ownership structures.
- Database migration included (
ownershipstable). - Works seamlessly with Laravel 9, 10, and 11.
📦 Installation
-
Require the package:
composer require dibakar/laravel-ownership(if you’re testing the dev branch, use
composer require dibakar/laravel-ownership:dev-main) -
Publish and run the migration:
php artisan vendor:publish --tag=ownership-migrations php artisan migrate -
Add the
HasOwnershiptrait to your models:use Dibakar\Ownership\Traits\HasOwnership; class Project extends Model { use HasOwnership; }
🔧 Usage Example
$project = Project::find(1); $user = User::find(1); // Assign ownership $project->addOwner($user, 'manager'); // Check ownership if ($project->isOwnedBy($user)) { echo "User owns this project!"; }
🤝 Why Use This Package?
- Keeps your code clean and DRY.
- Avoids writing repetitive relationship logic.
- Flexible enough to handle teams, clients, vendors, or multi-role ownership.
📚 Resources
License
This package is open-source and available under the MIT License.
Make ownership handling in Laravel easy and maintainable with dibakar/laravel-ownership. 🚀