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
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']