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:
- A user owning a blog post.
- A user and a team owning multiple files.
- Record being owned by many organizations.
- Composer
- Laravel Framework 5.7+/6.0+/7.0+
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
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;
}
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
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();
If you discover any security related issues, please use the issue tracker.
The MIT License (MIT). Please see License File for more information.