, (*1)
Laravel Owner
A simple package that allows Eloquent models to "own" each other, or "be owned" by another model. Supports many to many relationships., (*2)
Examples could include:, (*3)
- a user owning a blog post
- a user and a team owning multiple files
- record being owned by many organisations
Installation
Install using composer:, (*4)
composer require inventive/laravel-owner=dev-master
Add the following to config/app.php:, (*5)
Inventive\LaravelOwner\OwnerServiceProvider::class,
Publish the migrations and config files:, (*6)
php artisan vendor:publish
Run the migrations:, (*7)
php artisan migrate
Add necessary traits your Eloquent models:
If the model can be an owner:, (*8)
use Inventive\LaravelOwner\Traits\Owns;
class User extends Model
{
use Owns;
}
If the model can be owned by another model:, (*9)
use Inventive\LaravelOwner\Traits\HasOwner;
class Resource extends Model
{
use HasOwner;
}
Usage
"Owner" model:
Create an ownership:, (*10)
$user->own($model);
Remove an ownership:, (*11)
$user->disown($model);
Return a collection of all the models owned by the parent model:, (*12)
$user->owns();
Does the user own this model?, (*13)
$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., (*14)
$user->ownsModelType($model); // Use a model
$user->ownsModelType(āApp\Resourceā); // Use class name
"Owned" model:
Return a collection of all the model's owners:, (*15)
$model->owners();
Is the model is owned by another model?, (*16)
$model->isOwnedBy($owner);
Add an owner to the model:, (*17)
$model->addOwner($owner);
Remove an owner from the model, (*18)
$model->removeOwner($owner);
License
MIT, (*19)