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
Laravel Cloud

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

Visit Laravel Cloud
PhpStorm logo

PhpStorm

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

PhpStorm
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
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
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
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
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Lucky Media logo

Lucky Media

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

Lucky Media
Tinkerwell logo

Tinkerwell

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

Tinkerwell
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
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
Shift logo

Shift

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

Shift

The latest

View all →
A Free Shift to Check If Your App is Ready for Laravel Cloud image

A Free Shift to Check If Your App is Ready for Laravel Cloud

Read article
Laravel Idempotency: HTTP Idempotency Middleware for Laravel image

Laravel Idempotency: HTTP Idempotency Middleware for Laravel

Read article
Polyscope for Windows is Now Available image

Polyscope for Windows is Now Available

Read article
Laravel Sluggable image

Laravel Sluggable

Read article
Ship AI with Laravel: RAG with Embeddings and pgvector in Laravel 13 image

Ship AI with Laravel: RAG with Embeddings and pgvector in Laravel 13

Read article
Laravel Mobile Pass: Generate Apple Wallet and Google Wallet Passes image

Laravel Mobile Pass: Generate Apple Wallet and Google Wallet Passes

Read article