inventive / laravel-owner
Allow Eloquent models to own each other. Supports many to many relationships.
Requires
- inventive/laravel-owner: dev-master
This package is not auto-updated.
Last update: 2024-01-02 14:36:42 UTC
README
Laravel Owner
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 organisations
Installation
Install using composer:
composer require inventive/laravel-owner=dev-master
Add the following to config/app.php
:
Inventive\LaravelOwner\OwnerServiceProvider::class,
Publish the migrations and config files:
php artisan vendor:publish
Run the migrations:
php artisan migrate
Add necessary traits your Eloquent models:
If the model can be an owner:
use Inventive\LaravelOwner\Traits\Owns; class User extends Model { use Owns; }
If the model can be owned by another model:
use Inventive\LaravelOwner\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);
License
MIT