dev-master
9999999-devA status manager trait for Eloquent models
MIT
The Requires
The Development Requires
by Ido Bublil
0.1
0.1.0.0A status manager trait for Eloquent models
MIT
The Requires
The Development Requires
by Ido Bublil
Wallogit.com
2017 © Pedro Peláez
A status manager trait for Eloquent models
Handling status changes of a model is always a pain., (*2)
Eloquent Status Mutator provides is a simple trait which enforces correct status changes & some more cool features., (*3)
Define the statuses of the model in the statuses property:, (*4)
class Order extends Model
{
use HasStatus;
protected $statuses = [
'opened' => [],
'paid' => ['from' => 'opened'],
'approved' => ['from' => 'paid'],
'shipped' => ['from' => ['paid', 'approved']],
'arrived' => ['from' => 'shipped'],
'cancelled' => ['not-from' => ['arrived']],
];
}
The package makes sure that only listed statuses can be set:, (*5)
$order->status = 'opened'; // OK $order->status = 'some other status'; // Throws Exception
The package enforces that status can be set only after defined statuses in its 'from' key, (*6)
$order->status = 'opened'; $order->status = 'paid'; // OK $order->status = 'arrived'; // Throws Exception
The package also enforces the 'not-from' status, (*7)
$order->status = 'arrived'; $order->status = 'cancelled'; // Throws Exception
$order->status = 'paid';
if ($order->is('paid')) {
echo 'The order is shipped';
}
if ($order->canBe('shipped')) {
echo 'The order can be shipped';
}
In some cases, we need to do something after a specific status is set - for example, send a mail after an order is cancelled.
Our package invokes a method after status change by the convention of on + status name (camel cased), (*8)
class Order extends Model
{
use HasStatus;
protected $statuses = [
'opened' => [],
'paid' => ['from' => 'opened'],
'approved' => ['from' => 'paid'],
'shipped' => ['from' => ['paid', 'approved']],
'arrived' => ['from' => 'shipped'],
'cancelled' => ['not-from' => ['arrived']],
];
public function onCancelled()
{
// Send cancellation mail to the user
}
}
composer require laravel-israel/eloquent-status-mutator
HasStatus trait in your modelclass Order extends Model
{
use HasStatus;
}
protected $statuses = [
'opened' => [],
'paid' => ['from' => 'opened'],
'approved' => ['from' => 'paid'],
'shipped' => ['from' => ['paid', 'approved']],
'arrived' => ['from' => 'shipped'],
'cancelled' => ['not-from' => ['arrived']],
];
A status manager trait for Eloquent models
MIT
A status manager trait for Eloquent models
MIT