whilesmart / eloquent-shareables
Manage shareables
Package info
github.com/whilesmartphp/eloquent-shareables
pkg:composer/whilesmart/eloquent-shareables
Requires
- php: ^8.4
- laravel/framework: ^12.0
Requires (Dev)
- fakerphp/faker: ^1.24
- larastan/larastan: ^3.0
- laravel/pint: ^1.22
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.4
- phpmd/phpmd: @stable
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.0
- squizlabs/php_codesniffer: ^4.0
- zircote/swagger-php: ^5.1
This package is auto-updated.
Last update: 2026-08-17 18:05:57 UTC
README
Eloquent Shareables is a lightweight Laravel package that allows you to easily share Eloquent models with different audiences and owners. It supports assigning specific access levels, managing unique sharing tokens, and querying shared models.
Installation
You can install the package via Composer:
composer require whilesmart/eloquent-shareables
You should publish and run the migrations:
php artisan vendor:publish --tag="shareables-migrations"
php artisan migrate
You can optionally publish the config file:
php artisan vendor:publish --tag="shareables-config"
This will create a config/sharables.php file in your application directory.
Usage
Preparing Your Models
Add the Shareable trait to any Eloquent model you want to share:
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Whilesmart\Shareables\Traits\Shareable; class Post extends Model { use Shareable; }
1. Sharing Models
To share a model, call the share() method:
// Share a post with read-only access to a specific audience, owned by the current user $share = $post->share('read', $user, 'team_123'); // Share a post publicly with a custom or automatically generated token $publicShare = $post->share('view', $user); // A unique token (e.g. Str::random(40)) is generated if none is passed.
2. Checking Share Status
You can check if a model is currently shared with an audience or via a specific token:
// Check if shared with an audience (e.g. team_123) if ($post->isSharedWith('team_123')) { // Shared! } // Check if shared via a specific access token if ($post->isSharedViaToken($token)) { // Shared! }
3. Revoking Shares
Shares can be revoked either individually (for an audience) or all at once:
// Revoke sharing for a specific audience $post->revokeShare('team_123'); // Revoke all shares for this model $post->revokeAllShares();
4. Querying Shared Models
Use the sharedWith query scope to retrieve models shared with a specific audience:
$posts = Post::sharedWith('team_123')->get();
Testing
You can run the package test suite using:
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.