trinavo/laravel-ownable
Composer 安装命令:
composer require trinavo/laravel-ownable
包简介
A Laravel package for managing ownership of models.
README 文档
README
A Laravel package that adds ownership capabilities to your Eloquent models.
Installation
You can install the package via composer:
composer require trinavo/ownable
Migrations
After installing the package, you need to publish and run the migrations to create the necessary database tables:
php artisan vendor:publish --provider="Trinavo\Ownable\OwnableServiceProvider" --tag="migrations" php artisan migrate
Usage
Basic Usage
Add the Ownable trait to any Eloquent model that you want to make ownable:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Trinavo\Ownable\Traits\Ownable; class Post extends Model { use Ownable; // Your model code... }
Adding Owners
// Add the authenticated user as an owner $post->addOwner(Auth::user()); // Add another user as an owner $post->addOwner($anotherUser); // Add a team as an owner (any model can be an owner) $post->addOwner($team);
Checking Ownership
// Check if a model is owned by a specific user if ($post->isOwnedBy($user)) { // The user owns this post }
Removing Owners
// Remove a specific owner $post->removeOwner($user); // Remove all owners $post->removeOwners();
Query Scopes
The package includes helpful query scopes:
// Get all posts owned by a specific user $userPosts = Post::ownedBy($user)->get(); // Get all posts owned by the currently authenticated user $myPosts = Post::mine()->get();
Getting Owners
// Get all owners of a model $owners = $post->getOwners(); // Get owners of a specific type $userOwners = $post->getOwners(User::class); $userOwners = $post->getOwners([User::class, Admin::class]); // Get first owner (useful when you expect only one owner) $mainOwner = $post->getOwner();
Auto-Ownership
By default, the trait automatically assigns the currently authenticated user as an owner when a model is created. You can disable this behavior:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Trinavo\Ownable\Traits\Ownable; class Post extends Model { use Ownable; public static $autoOwn = false; // Your model code... }
Features
- Support for multiple owners per model
- Different types of owners (users, teams, etc.)
- Automatic cleanup of ownership records when models are deleted
- Convenient query scopes
- Easy ownership assignment and checking
License
This package is open-sourced software licensed under the MIT license.
Support the Developer
If you find this package useful, please consider supporting the developer:
trinavo/laravel-ownable 适用场景与选型建议
trinavo/laravel-ownable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 03 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 trinavo/laravel-ownable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 trinavo/laravel-ownable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 7
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-18