PHP code example of bernskiold / laravel-collaboration

1. Go to this page and download the library: Download bernskiold/laravel-collaboration 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/ */

    

bernskiold / laravel-collaboration example snippets


$document->shareWith($ada, SharePermission::Edit);

$document->isSharedWith($ada);                          // true
$document->isSharedWith($ada, SharePermission::Edit);   // true
Document::query()->sharedWith($ada)->get();

$document->stopSharingWith($ada);

Relation::enforceMorphMap([
    'share' => \Bernskiold\LaravelCollaboration\Models\Share::class,
    // ...
]);

use Bernskiold\LaravelCollaboration\Concerns\CanBeShared;
use Bernskiold\LaravelCollaboration\Contracts\Shareable;

class Document extends Model implements Shareable
{
    use CanBeShared;

    public function shareOwnerColumn(): ?string
    {
        return 'owner_id';
    }
}

public function sharePermissions(): array
{
    return [SharePermission::View];
}

class DocumentPolicy
{
    public function view(User $user, Document $document): bool
    {
        return $document->isOwnedBy($user) || $document->isSharedWith($user);
    }

    public function update(User $user, Document $document): bool
    {
        return $document->isOwnedBy($user) || $document->isSharedWith($user, SharePermission::Edit);
    }

    public function share(User $user, Document $document): bool
    {
        return $document->isOwnedBy($user);
    }
}

Document::query()->sharedWith($user)->get();                          // shared with the user
Document::query()->sharedWith($user, SharePermission::Edit)->get();   // shared at edit
Document::query()->ownedBy($user)->get();                             // the user is the owner
Document::query()->ownedByOrSharedWith($user)->get();                 // the user is the owner, or it is shared with the user

// One person. The other shares do not change.
$record->shareWith($user, SharePermission::View, $sharedBy);
$record->stopSharingWith($user, $removedBy);

// The full list. The package removes the access of each person that is not in the list.
$record->syncShares([
    $ada->id => SharePermission::Edit,
    $alan->id => SharePermission::View,
], $performedBy);

if ($record->shareWith($user, SharePermission::Edit) === ShareOutcome::Unchanged) {
    // The user already had edit access.
}

public function shareNotification(Model $sharedWith, ?Model $sharedBy, SharePermission $permission): ?Notification
{
    return new DocumentShared($this, $sharedBy);
}

ShareActivity::describe($entry); // ['verb' => 'shared it with', 'object' => 'Ada Lovelace, Alan Turing']

use Bernskiold\LaravelCollaboration\Collaboration;

$record = Collaboration::findShareable($this->type, $this->key);

abort_if($record === null, 404);

$this->authorize('share', $record);

$people = Collaboration::people()->search($term, $record, limit: 15);

$person = Collaboration::people()->resolve($this->person);

if ($person === null || $record->isOwnedBy($person)) {
    $this->addError('person', __('Select a person.'));

    return;
}

$record->shareWith($person, SharePermission::from($this->permission));

class ActivePeople extends PeopleDirectory
{
    protected function query(): Builder
    {
        return User::query()->active();
    }
}

public function canInvite(): bool
{
    return true;
}

public function invite(string $email): ?Model
{
    return User::query()->firstOrCreate(['email' => $email], [/* ... */]);
}

foreach ($record->sharePermissions() as $permission) {
    $permission->value;          // "view"
    $permission->label();        // "Can view"
    $permission->description();  // "Can open it, but cannot change it."
}

Share::factory()->on($document)->sharedWith($user)->edit()->create();

app(HandOverShares::class)->execute(from: $duplicate, to: $user);

return [
    'user_model' => null,                     // null: the model of the default auth provider
    'share_model' => Share::class,
    'table' => 'shares',
    'connection' => null,                     // null: the default connection
    'people' => PeopleDirectory::class,
    'notifications' => ['enabled' => true, 'default' => SharedWithYou::class],
    'activity_log' => ['enabled' => true, 'log_name' => null], // null: the morph class of the record
];
bash
php artisan vendor:publish --tag=collaboration-migrations
bash
php artisan migrate
bash
php artisan vendor:publish --tag=collaboration-translations
bash
php artisan vendor:publish --tag=collaboration-config