New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

peltonsolutions/laravel-ownerable

A custom package for Laravel to make it easier to attach and require classes to belong to a tenant.
23,869

Larablocks Analysis

peltonsolutions/laravel-ownerable works with Laravel 11, 12 and 13 (latest release v1.1.0, Nov 27, 2026).

Recommendation checks

Solid choice

Solid choice: maintained and up to date, meets 2 of 4 checks; below the bar for Recommended on use.

  • Works with Laravel 13, the current version
  • Released in the last 12 months (Nov 27, 2026)
  • 1,461 downloads a month (needs 10,000)
  • 0 GitHub stars (needs 100)

State

Actively maintained
  • Last stable release: Nov 27, 2026.
  • 4 releases in the last 12 months.
  • First released: May 24, 2026.
  • Monthly downloads: 1,461.
  • 3 open issues on GitHub.

Data from Packagist and GitHub, checked by Larablocks on Nov 30, 2026.

Install
composer require peltonsolutions/laravel-ownerable
Latest Version:v1.1.0
PHP:^8.4
Laravel:11 · 12 · 13
License:MIT
Last Updated:Nov 27, 2026
Links: GitHub  ·  Packagist
Maintainer: peltonsolutions

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:

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();

Permanent deletes by query

Models that use CanBePossessed get an OwnerableBuilder, whose forceDelete() on a query respects the owner scope, as delete() does:

  • Run for the current owner, it deletes only that owner's matching rows, soft-deleted ones included.
  • With no current owner, it deletes only rows that have no owner, as delete() does.
  • After withoutGlobalScope('owner') or withoutGlobalScopes(), it deletes across owners, as before.

A record's own $post->forceDelete() deletes that record, as before.

Models with their own builder

A model that sets its own Eloquent builder (a $builder property, or #[UseEloquentBuilder] on Laravel 12 and later) must have that builder extend PeltonSolutions\LaravelOwnerable\Builders\OwnerableBuilder. Otherwise building a query for it throws a LogicException. A model that overrides newEloquentBuilder() itself must return an OwnerableBuilder too. How PHP checks this depends on where the model gets CanBePossessed:

  • The model's own class uses the trait: PHP checks the override only when it declares : OwnerableBuilder as its return type, and returning another builder then throws a TypeError. An override with any other return type, or none, loads and is not checked. If it returns another builder, a query-level forceDelete() on that model does not respect the owner scope.
  • The model inherits the trait from a parent class: the override must be compatible with : OwnerableBuilder, or the model fails to load.

A model that also uses another trait defining newEloquentBuilder() fails to load with a trait collision error, and the model must resolve the collision itself.

use PeltonSolutions\LaravelOwnerable\Builders\OwnerableBuilder;

class BlogPostBuilder extends OwnerableBuilder
{
    // ...
}

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 — and, against an in-memory SQLite database, which rows a query-level forceDelete() reaches.

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.

Related Packages

peltonsolutions/filament-common

A custom package for Filament that provides common enhancements used throughout...

10,456 0
doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

670,032,257 9,703
laravel/framework

The Laravel Framework.

640,572,096 35,338
laravel/tinker

Powerful REPL for the Laravel framework.

546,909,908 7,440
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

465,816,364 607

Version History

Version Released PHP Laravel License
v1.1.0 ^8.4 ^11.0 || ^12.0 || ^13.0 MIT
v1.0.2 ^8.4 ^11.0|^12.0|^13.0 MIT
v1.0.1 ^8.4 ^11.0|^12.0|^13.0 MIT
v1.0.0 ^8.4 ^11.0|^12.0|^13.0 MIT