›
byrcsc/laravel-assignment · 1.x
Install Laravel Assignment, create its tables, and add the two traits to your models.
composer require byrcsc/laravel-assignment
php artisan vendor:publish --tag="assignment-migrations"
php artisan migrateThe migration creates three tables:
| Table | What it holds |
|---|---|
assignments | One row per assignment, open or ended. This is the history |
assignment_scopes | Rotation state for stateful policies, one row per scope string |
assignment_queue | Assignables waiting for a candidate, one row per assignable and role |
Publish the configuration before running the migration when your models do not use integer keys, or when you want different table names. The migration reads both from config, so changing them afterwards means altering tables by hand.
php artisan vendor:publish --tag="assignment-config"Set the key type for either side of an assignment when its models use uuid,
ulid, or string keys:
ASSIGNMENT_ASSIGNABLE_KEY_TYPE=uuid
ASSIGNMENT_ASSIGNEE_KEY_TYPE=ulidBoth default to id, which is an unsigned big integer. The setting is one
value per side rather than one per model, so every assignable shares a key type
and every assignee shares another. See
configuration.
use ByRcsc\LaravelAssignment\Concerns\Assignable;
use ByRcsc\LaravelAssignment\Concerns\Assignee;
class Enquiry extends Model
{
use Assignable;
}
class Tradie extends Model
{
use Assignee;
}Neither trait adds columns to your tables. Both read and write the
assignments table through polymorphic columns, so a model can be assignable,
an assignee, or both.
Morph maps are honoured. If Relation::enforceMorphMap() is active, the alias
is what lands in assignable_type and assignee_type.
One command expires overdue offers, advances their cascades, and flushes the queue:
// routes/console.php
use Illuminate\Support\Facades\Schedule;
Schedule::command('assignment:tick')->everyMinute();The schedule is optional for correctness. An overdue offer stops holding its
slot the moment its expires_at passes, whether or not the command has run.
What the command does is advance the cascade to the next candidate and pick up
queued work. See expiry and scheduling.
The package notifies the assignee at four moments. Mail is the only channel on by default, because it is the only one that needs nothing installed:
// config/assignment.php
'notification_channels' => ['mail'],Adding 'database' requires Laravel's notifications table:
php artisan make:notifications-table
php artisan migrateAssignees that do not use Laravel's Notifiable trait are skipped without an
error. See notifications.
Two more tags exist, and neither is needed to run the package:
php artisan vendor:publish --tag="assignment-translations"
php artisan vendor:publish --tag="assignment-views"Publish them to change the notification wording or the mail markup. See notifications.
$enquiry = Enquiry::query()->create([]);
$tradie = Tradie::query()->create([]);
$enquiry->assign($tradie);
$enquiry->assignee()->is($tradie); // true
$enquiry->assignments()->count(); // 1If assign() throws SlotOccupied, the slot already holds an open row. That
is the constraint doing its job. See exceptions.