The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

Handle ownership relationships between Eloquent models with Laravel Ownable

Last updated on by

Handle ownership relationships between Eloquent models with Laravel Ownable image

The Laravel Ownable package, created by Abdullah Sowailem, provides a flexible and straightforward way to manage ownership of any model by another model in your Laravel application.

Key Features

  • Flexible Ownership: Any model can own any other model
  • Ownership Transfer: Transfer ownership between different owners
  • Ownership History: Keep track of ownership changes over time
  • Current Owner: Easily retrieve the current owner of any ownable item
  • Bulk Operations: Check ownership status and manage multiple ownables
  • Automatic Cleanup: Automatically clean up ownership records when models are deleted
  • Facade Support: Use the convenient Owner facade for ownership operations

Example Usage

Let's say we have User and Task models. With the Laravel Ownable package, we can represent ownership by using the Owner and Ownable contracts along with the HasOwnables and isOwnable traits in our respective models.

use Sowailem\Ownable\Traits\HasOwnables;
use Sowailem\Ownable\Contracts\Owner as OwnerContract;
 
// Owner model (e.g., User)
class User extends Authenticatable implements OwnerContract
{
use HasOwnables;
}
use Sowailem\Ownable\Traits\IsOwnable;
use Sowailem\Ownable\Contracts\Ownable as OwnableContract;
 
// Ownable Model
class Task extends Model implements OwnableContract
{
use IsOwnable;
}

With that now in place, you have several methods available on your models to give, take, remove, and check ownership.

$user = User::first();
$task = Task::first();
 
// Give a user ownership of a task
$user->giveOwnershipTo($task);
 
$task->ownedBy($user);
 
if ($task->isOwnedBy($user)) {
// Check if the user owns the task
}
 
// Transfer ownership to another user
$someOtherUser = User::find(2);
$user->transferOwnership($task, $someOtherUser);
 
// Get the current owner of an item
$currentOwner = $task->currentOwner();
 
// Get all owners (including historical ownership)
$allOwnersOfTheTask = $task->owners()->get();
 
// Remove Ownership
$user->takeOwnershipFrom($task);

If you prefer to use the Facade implementation, it is also available.

use Illuminate\Support\Facades\Log;
use Sowailem\Ownable\Facades\Owner;
 
$user = User::find(4);
$task = Task::find(2);
 
Owner::give($user, $task);
 
if (Owner::check($user, $task)) {
Log::info("User {$user->id} is the owner of task {$task->id}");
}
 
$newOwner = User::find(1);
 
Owner::transfer($user, $newOwner, $task);
Log::info("Task {$task->id} is now owned by {$task->currentOwner()->name}");

Another nice feature of this package is that it includes a migration that helps keep track of changes in the ownership of your Models. The entries in your database look similar to this:

Learn more about this package and view the source code on GitHub.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Ship AI with Laravel: Stop Your AI Agent from Guessing image

Ship AI with Laravel: Stop Your AI Agent from Guessing

Read article
Laravel Cloud Adds Path Blocking to Prevent Bots From Waking Hibernated Apps image

Laravel Cloud Adds Path Blocking to Prevent Bots From Waking Hibernated Apps

Read article
Making Laravel MongoDB Operations Idempotent: Safe Retries for Financial Transactions image

Making Laravel MongoDB Operations Idempotent: Safe Retries for Financial Transactions

Read article
FormRequest Strict Mode and Queue Job Inspection in Laravel 13.4.0 image

FormRequest Strict Mode and Queue Job Inspection in Laravel 13.4.0

Read article
Pretty PHP Info: A Modern Replacement for `phpinfo()` image

Pretty PHP Info: A Modern Replacement for `phpinfo()`

Read article
Laracon US 2026 Announced image

Laracon US 2026 Announced

Read article