Skip to content

Commit 9b23801

Browse files
dionysiosarvanitisAnton Komarev
authored and
Anton Komarev
committed
Add section for owner model. Minor linting fixes (#17)
Add section for Owner model
1 parent ab1e142 commit 9b23801

File tree

1 file changed

+42
-28
lines changed

1 file changed

+42
-28
lines changed

README.md

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@
1010
## Introduction
1111

1212
Laravel Ownership simplify management of eloquent model's owner. Group can be an owner of event, user can be an owner of chat room, organization can own licenses. It can be used for many cases not limited by authorship. Make any model as owner and create ownable models in a minutes!
13-
14-
## Contents
15-
16-
- [Features](#features)
17-
- [Installation](#installation)
18-
- [Usage](#usage)
19-
- [Prepare ownable model with strict ownership](#prepare-ownable-model-with-strict-ownership)
20-
- [Prepare ownable model with polymorphic ownership](#prepare-ownable-model-with-polymorphic-ownership)
21-
- [Available methods](#available-methods)
22-
- [Scopes](#scopes)
23-
- [Set authenticated user as owner automatically](#set-authenticated-user-as-owner-automatically)
24-
- [Change log](#change-log)
25-
- [Upgrading](#upgrading)
26-
- [Contributing](#contributing)
27-
- [Testing](#testing)
28-
- [Security](#security)
29-
- [Credits](#credits)
30-
- [Alternatives](#alternatives)
31-
- [License](#license)
32-
- [About CyberCog](#about-cybercog)
13+
14+
## Contents
15+
16+
- [Features](#features)
17+
- [Installation](#installation)
18+
- [Usage](#usage)
19+
- [Prepare ownable model with strict ownership](#prepare-ownable-model-with-strict-ownership)
20+
- [Prepare ownable model with polymorphic ownership](#prepare-ownable-model-with-polymorphic-ownership)
21+
- [Available methods](#available-methods)
22+
- [Scopes](#scopes)
23+
- [Set authenticated user as owner automatically](#set-authenticated-user-as-owner-automatically)
24+
- [Change log](#change-log)
25+
- [Upgrading](#upgrading)
26+
- [Contributing](#contributing)
27+
- [Testing](#testing)
28+
- [Security](#security)
29+
- [Credits](#credits)
30+
- [Alternatives](#alternatives)
31+
- [License](#license)
32+
- [About CyberCog](#about-cybercog)
3333

3434
## Features
3535

@@ -70,13 +70,27 @@ Laravel Ownership allows model to have strict owner model type (`HasOwner` trait
7070

7171
Strict ownership is useful when model can belongs to only one model type. Attempt to set owner of not defined model type will throw an exception `InvalidOwnerType`.
7272
*Example: Only users allowed to create posts.*
73-
73+
7474
Polymorphic ownership is useful when model can belongs to owners of different types.
7575
*Example: Users and Organizations can upload applications to marketplace.*
7676

77+
### Prepare owner model
78+
79+
At the owner model use `CanBeOwner` contract and implement it:
80+
81+
```php
82+
use Cog\Contracts\Ownership\CanBeOwner;
83+
use Illuminate\Foundation\Auth\User as Authenticatable;
84+
85+
class User extends Authenticatable implements CanBeOwner
86+
{
87+
// ...
88+
}
89+
```
90+
7791
### Prepare ownable model with strict ownership
7892

79-
Use `Ownable` contract in model which will get ownership behavior and implement it or just use `HasOwner` trait.
93+
Use `Ownable` contract in model which will get ownership behavior and implement it or just use `HasOwner` trait.
8094

8195
```php
8296
use Cog\Contracts\Ownership\Ownable as OwnableContract;
@@ -94,7 +108,7 @@ Ownable model with strict ownership must have in database additional nullable co
94108
```php
95109
Schema::table('articles', function (Blueprint $table) {
96110
$table->integer('owned_by_id')->unsigned()->nullable();
97-
111+
98112
$table->index('owned_by_id');
99113
});
100114
```
@@ -122,7 +136,7 @@ class Article extends Model implements OwnableContract
122136

123137
### Prepare ownable model with polymorphic ownership
124138

125-
Use `Ownable` contract in model which will get polymorphic ownership behavior and implement it or just use `HasMorphOwner` trait.
139+
Use `Ownable` contract in model which will get polymorphic ownership behavior and implement it or just use `HasMorphOwner` trait.
126140

127141
```php
128142
use Cog\Contracts\Ownership\Ownable as OwnableContract;
@@ -137,21 +151,21 @@ class Article extends Model implements OwnableContract
137151

138152
Ownable model with polymorphic ownership must have in database additional nullable columns to store owner relation:
139153

140-
**Laravel 5.3.29 and newer**
154+
#### Laravel 5.3.29 and newer
141155

142156
```php
143157
Schema::table('articles', function (Blueprint $table) {
144158
$table->nullableMorphs('owned_by');
145159
});
146160
```
147161

148-
**Laravel 5.3.28 and older**
162+
#### Laravel 5.3.28 and older
149163

150164
```php
151165
Schema::table('articles', function (Blueprint $table) {
152166
$table->integer('owned_by_id')->unsigned()->nullable();
153167
$table->string('owned_by_type')->nullable();
154-
168+
155169
$table->index([
156170
'owned_by_id',
157171
'owned_by_type',
@@ -273,7 +287,7 @@ class Article extends Model implements OwnableContract
273287
use HasOwner;
274288

275289
public $withDefaultOwnerOnCreate = true;
276-
290+
277291
/**
278292
* Resolve entity default owner.
279293
*

0 commit comments

Comments
 (0)