Download the PHP package peltonsolutions/laravel-ownerable without Composer
On this page you can find all versions of the php package peltonsolutions/laravel-ownerable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download peltonsolutions/laravel-ownerable
More information about peltonsolutions/laravel-ownerable
Files in peltonsolutions/laravel-ownerable
Package laravel-ownerable
Short Description A custom package for Laravel to make it easier to attach and require classes to belong to a tenant.
License MIT
Homepage https://github.com/peltonsolutions/laravel-ownerable
Informations about the package laravel-ownerable
laravel-ownerable
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:
Publish the config file:
This creates config/ownerable.php:
Set the owner class in your .env:
Usage
1. Implement CanHavePossessions on the owner model
The owner model (e.g. Account) must implement the CanHavePossessions contract:
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).
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
Bypassing the global scope
Use withoutGlobalScope when you need to query across all owners:
Testing
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 [email protected] instead of using the issue tracker.
Credits
License
laravel-ownerable is open-sourced software licensed under the MIT license.