Many applications let a person share a record with a colleague. It can be a document, a report or a project. The feature looks small, but it has many parts. You need a table, permissions, notifications and a history of the changes. You also need the same rules in each place where the shares change.

We built this feature in many client applications. Now it is a package: Laravel Collaboration.

What the package does

The owner of a record can share it with other users. Each user gets a permission: "view" or "edit". Your policy decides what each permission lets a user do.

  • It keeps the shares in one table, with one row for each user and record.
  • It writes all changes with one action. Each change has the same rules, the same events and the same activity entries.
  • It sends a notification to each user that gets access.
  • It records each change in the activity log of the record, if spatie/laravel-activitylog is installed.
  • It moves the shares of one user to a different user, for example when you merge two accounts.
  • It has no views and no components. You build the interface with the stack that you use.

Install the package

composer require bernskiold/laravel-collaboration

Publish and run the migration:

php artisan vendor:publish --tag=collaboration-migrations
php artisan migrate

Make a model shareable

Implement the Shareable contract and use the CanBeShared trait. Then tell the package which column keeps the owner.

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';
    }
}

Now you can share the record:

$document->shareWith($ada, SharePermission::Edit);
$document->isSharedWith($ada); // true
$document->stopSharingWith($ada);

To set the full list of people in one step, use syncShares(). The package removes the access of each person that is not in the list.

$document->syncShares([
    $ada->id => SharePermission::Edit,
    $alan->id => SharePermission::View,
]);

shareWith() returns an outcome: Added, Changed, Removed or Unchanged. Use it to tell the user what happened.

Use the permissions in a policy

The package does not give access. Your policy reads the shares and makes the decision:

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);
    }
}

If you eager-load the shares, isSharedWith() uses the loaded shares. Thus a list does not make one query for each record.

Find records

The trait gives scopes to find the records of a user:

Document::query()->sharedWith($user)->get();
Document::query()->ownedBy($user)->get();
Document::query()->ownedByOrSharedWith($user)->get();

Each scope accepts a model, a key or null. With null, the scope finds no records. Thus you can give auth()->user() also when nobody is signed in.

Notifications and events

When a user gets access, the package sends a queued email with a link to the record. To send a different notification for one type of record, override shareNotification(). Return null to send no notification.

The package also dispatches events: RecordShared, SharePermissionChanged and RecordUnshared. It dispatches them after the database transaction commits. Use them to refresh your interface or to clear a cache.

Build the interface

The package has helpers that keep your components small. findShareable() finds a record from a morph type and a key. The people directory searches users by name and email address, and it does not show the owner.

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);

Users can also share a record with an email address that has no account. To allow this, extend the people directory and override canInvite() and invite().

A limit to know

The sharedWith() scope uses whereHas(). For this reason, the shares table must be on the same database server as the table of the record.

Get the package

Laravel Collaboration is open source, with the MIT license. The code and the full documentation are on GitHub. If you find a problem or have an idea, open an issue. We are happy to hear from you.