1. Go to this page and download the library: Download dibakar/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/ */
dibakar / laravel-ownership example snippets
return [
/*
|--------------------------------------------------------------------------
| Morph Name
|--------------------------------------------------------------------------
|
| This is the name of the polymorphic relationship used for ownership.
| You can change this to 'user', 'team', 'organization', etc.
*/
'morph_name' => 'owner',
/*
|--------------------------------------------------------------------------
| Global Scope
|--------------------------------------------------------------------------
|
| When enabled, a global scope will be applied to automatically scope
| queries to the current owner in single ownership mode.
*/
'apply_global_scope' => true,
/*
|--------------------------------------------------------------------------
| Authentication Guard
|--------------------------------------------------------------------------
|
| The authentication guard used to retrieve the currently authenticated user.
*/
'guard' => 'web',
/*
|--------------------------------------------------------------------------
| Ownership Mode
|--------------------------------------------------------------------------
|
| Set to 'single' for one owner per model or 'multiple' for many owners.
*/
'mode' => 'single',
/*
|--------------------------------------------------------------------------
| Cache Configuration
|--------------------------------------------------------------------------
*/
'cache' => [
'enabled' => true,
'ttl' => 3600, // Cache time-to-live in seconds
],
/*
|--------------------------------------------------------------------------
| Multiple Ownership Configuration
|--------------------------------------------------------------------------
*/
'multiple_ownership' => [
'table_name' => 'ownerships',
'roles' => [
'owner' => [
'display_name' => 'Owner',
'permissions' => ['*'], // Wildcard means all permissions
],
'editor' => [
'display_name' => 'Editor',
'permissions' => ['edit', 'view'],
],
'viewer' => [
'display_name' => 'Viewer',
'permissions' => ['view'],
],
],
'default_role' => 'viewer',
],
];
use Illuminate\Database\Eloquent\Model;
use Dibakar\Ownership\Traits\HasOwnership;
class Post extends Model
{
use HasOwnership;
// ...
}
// Creating a new post with the current user as owner
$post = Post::create([
'title' => 'My First Post',
'content' => 'This is my first post.'
]);
// Explicitly set the owner
$post->setOwner($user);
// Check ownership
if ($post->isOwnedBy($user)) {
// User owns the post
}
// Get the owner
$owner = $post->owner;
// Transfer ownership
$post->transferOwnership($currentOwner, $newOwner);
// Clear the owner
$post->clearOwner();
// config/ownership.php
return [
'mode' => 'multiple',
// ... rest of the config
];
use Illuminate\Database\Eloquent\Model;
use Dibakar\Ownership\Traits\HasOwnership;
class Project extends Model
{
use HasOwnership;
// ...
}
// Add an owner with a specific role
$project->addOwner($user, 'owner');
// Add multiple owners at once
$project->addOwners([$user1, $user2, $user3], 'editor', ['custom_permission']);
// Remove an owner
$project->removeOwner($user);
// Check if a user is an owner
if ($project->hasOwner($user)) {
// User is an owner
}
// Get all owners with their roles
$owners = $project->getOwners();
// Get owners with a specific role
$editors = $project->getOwnersWithRole('editor');
// Check if a user has a specific role
if ($project->hasOwnerWithRole($user, 'admin')) {
// User has admin role
}
// Update a user's role
$project->updateOwnerRole($user, 'admin');
// Sync owners (removes any owners not in the list)
$project->syncOwners([
$user1,
$user2,
$user3,
], 'owner');
// Clear all owners
$project->clearAllOwners();
}
### Query Scopes
The package provides several query scopes to filter models by ownership:
use Dibakar\Ownership\Events\OwnershipCreated;
use Dibakar\Ownership\Events\OwnershipUpdated;
use Dibakar\Ownership\Events\OwnershipDeleted;
use Dibakar\Ownership\Events\OwnershipTransferred;
// Listen for ownership events
Event::listen(OwnershipCreated::class, function (OwnershipCreated $event) {
$model = $event->model;
$owner = $event->owner;
$role = $event->role;
// Handle the event
});
namespace App\Listeners;
use Dibakar\Ownership\Events\OwnershipCreated;
class LogOwnershipCreated
{
public function handle(OwnershipCreated $event)
{
// Handle the event
}
}