pandorga/owner

Allow Eloquent models to own each other. Supports many to many relationships.

1.2 2020-12-16 18:53 UTC

This package is auto-updated.

Last update: 2024-05-17 01:49:47 UTC


README

A simple package that allows Eloquent models to "own" each other, or "be owned" by another model. Supports many-to-many relationships.

Examples could include:

  1. A user owning a blog post.
  2. A user and a team owning multiple files.
  3. Record being owned by many organizations.

Installation

Requirements

  • Composer
  • Laravel Framework 5.7+/6.0+/7.0+

Installing

Run the following command in your console terminal:

$ composer require pandorga/owner

Publish the migrations and config files:

$ php artisan vendor:publish --provider="Pandorga\Owner\OwnerServiceProvider"

Run the migrations:

$ php artisan migrate

Usage

Add necessary traits your Eloquent models:

If the model can be an owner:

use Pandorga\Owner\Traits\Owns;
    
class User extends Model
{
    use Owns;
}

If the model can be owned by another model:

use Pandorga\Owner\Traits\HasOwner;
    
class Resource extends Model
{
    use HasOwner;
}

Usage

"Owner" model:

Create an ownership:

$user->own($model);

Remove an ownership:

$user->disown($model);

Return a collection of all the models owned by the parent model:

$user->owns();

Does the user own this model?

$user->ownsModel($model);

Which models of this type does the parent model own? This method either takes a child model, or a name-spaced class name.

$user->ownsModelType($model); // Use a model
$user->ownsModelType(‘App\Resource’); // Use class name

"Owned" model:

Return a collection of all the model's owners:

$model->owners();

Is the model is owned by another model?

$model->isOwnedBy($owner);

Add an owner to the model:

$model->addOwner($owner);

Remove an owner from the model

$model->removeOwner($owner);

Remove all owners from the model

$model->removeAllOwners();

Security

If you discover any security related issues, please use the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.