PHP code example of cybercog / laravel-ownership

1. Go to this page and download the library: Download cybercog/laravel-ownership library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cybercog / laravel-ownership example snippets


'providers' => [
    Cog\Laravel\Ownership\Providers\OwnershipServiceProvider::class,
];

use Cog\Contracts\Ownership\CanBeOwner as CanBeOwnerInterface;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements CanBeOwnerInterface
{
    // ...
}

use Cog\Contracts\Ownership\Ownable as OwnableInterface;
use Cog\Laravel\Ownership\Traits\HasOwner;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements OwnableInterface
{
    use HasOwner;
}

Schema::table('articles', function (Blueprint $table) {
    $table->integer('owned_by_id')->unsigned()->nullable();

    $table->index('owned_by_id');
});

use Cog\Contracts\Ownership\Ownable as OwnableInterface;
use Cog\Laravel\Ownership\Traits\HasOwner;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements OwnableInterface
{
    use HasOwner;

    protected $ownerModel = Group::class;
    protected $ownerPrimaryKey = 'gid';
    protected $ownerForeignKey = 'group_id';
}

use Cog\Contracts\Ownership\Ownable as OwnableInterface;
use Cog\Laravel\Ownership\Traits\HasMorphOwner;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements OwnableInterface
{
    use HasMorphOwner;
}

Schema::table('articles', function (Blueprint $table) {
    $table->nullableMorphs('owned_by');
});

$article->ownedBy();
$article->owner();

$article->getOwner();
$article->ownedBy;
$article->owner;

$article->changeOwnerTo($owner);

$article->abandonOwner();

$article->hasOwner();

$article->isOwnedBy($owner);

$article->isNotOwnedBy($owner);

$article = new Article;
$article->withDefaultOwner()->save();

$user = User::where('name', 'admin')->first();
$article = new Article;
$article->withDefaultOwner($user)->save();

$article = new Article;
$article->withoutDefaultOwner()->save();

Article::whereOwnedBy($owner)->get();

Article::whereNotOwnedBy($owner)->get();

use Cog\Contracts\Ownership\Ownable as OwnableInterface;
use Cog\Laravel\Ownership\Traits\HasOwner;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements OwnableInterface
{
    use HasOwner;

    protected $withDefaultOwnerOnCreate = true;
}

use Cog\Contracts\Ownership\Ownable as OwnableInterface;
use Cog\Laravel\Ownership\Traits\HasOwner;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements OwnableInterface
{
    use HasOwner;

    public $withDefaultOwnerOnCreate = true;

    /**
     * Resolve entity default owner.
     * 
     * @return \Cog\Contracts\Ownership\CanBeOwner|null
     */
    public function resolveDefaultOwner()
    {
        return \App\User::where('name', 'admin')->first();
    }
}