PHP code example of laravel-israel / eloquent-status-mutator

1. Go to this page and download the library: Download laravel-israel/eloquent-status-mutator 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/ */

    

laravel-israel / eloquent-status-mutator example snippets


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

$order->status = 'opened'; // OK

$order->status = 'some other status'; // Throws Exception

$order->status = 'opened';

$order->status = 'paid'; // OK

$order->status = 'arrived'; // Throws Exception

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

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

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