peltonsolutions/laravel-ownerable
Composer 安装命令:
composer require peltonsolutions/laravel-ownerable
包简介
A custom package for Laravel to make it easier to attach and require classes to belong to a tenant.
关键字:
README 文档
README
A Laravel package by Pelton Solutions that makes it easy to attach models to an owner (tenant) and automatically scope queries to that owner.
A common use case is multi-tenant applications — for example, a BlogPost that belongs to an Account. The package auto-assigns the owner on creation and filters all queries to the current owner.
Requirements
- PHP ^8.4
- Laravel ^11.0
Installation
Install via Composer:
composer require peltonsolutions/laravel-ownerable
Publish the config file:
php artisan vendor:publish --provider="PeltonSolutions\LaravelOwnerable\LaravelOwnerableServiceProvider"
This creates config/ownerable.php:
return [ 'owner' => env('PELTON_SOLUTIONS_OWNER_CLASS'), ];
Set the owner class in your .env:
PELTON_SOLUTIONS_OWNER_CLASS=App\Models\Account
Usage
1. Implement CanHavePossessions on the owner model
The owner model (e.g. Account) must implement the CanHavePossessions contract:
use PeltonSolutions\LaravelOwnerable\Contracts\CanHavePossessions; class Account extends Model implements CanHavePossessions { // Return the currently active owner instance (e.g. from session or auth) public static function getCurrent(): ?static { return static::find(session('account_id')); } // Associate a possessed model with this owner public function addPossession(CanBePossessed $possession) { $possession->ownerable()->associate($this)->save(); } public function getKey() { return $this->id; } }
2. Use the CanBePossessed trait on owned models
Add the CanBePossessed trait (and optionally implement the contract) to any model that should be scoped to an owner. The model must have ownerable_id and ownerable_type columns (polymorphic morph).
use PeltonSolutions\LaravelOwnerable\Contracts\CanBePossessed as CanBePossessedContract; use PeltonSolutions\LaravelOwnerable\Traits\CanBePossessed; class BlogPost extends Model implements CanBePossessedContract { use CanBePossessed; }
What happens automatically
- On creation: If the model has no owner yet and a current owner is available, the owner is automatically associated via
ownerable()->associate($owner). - On all queries: A global scope filters results to only records owned by the current owner. If no current owner is found, the query returns no results (
ownerable_id = null).
Migration example
Schema::create('blog_posts', function (Blueprint $table) { $table->id(); $table->nullableMorphs('ownerable'); // ... other columns $table->timestamps(); });
Bypassing the global scope
Use withoutGlobalScope when you need to query across all owners:
BlogPost::withoutGlobalScope('owner')->get();
Testing
composer test
The test suite uses Pest and covers the CanBePossessed trait and observer — including owner resolution, query scoping, and auto-association on creation.
Security
If you discover any security-related issues, please email security@peltonsolutions.com instead of using the issue tracker.
Credits
License
laravel-ownerable is open-sourced software licensed under the MIT license.
peltonsolutions/laravel-ownerable 适用场景与选型建议
peltonsolutions/laravel-ownerable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 309 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 09 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「peltonsolutions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 peltonsolutions/laravel-ownerable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 peltonsolutions/laravel-ownerable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 peltonsolutions/laravel-ownerable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom package for Filament that provides common enhancements used throughout peltonsolutions packages.
Alfabank REST API integration
A custom package for Filament for handling enum values in Filament Admin.
A custom package for Laravel for handling enum values on Models.
A custom package for Laravel to assist with creating BelongsToMany pivot migrations.
Laravel package for Accurate Online API integration.
统计信息
- 总下载量: 309
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-25