PHP code example of orobogenius / laravel-model-statable

1. Go to this page and download the library: Download orobogenius/laravel-model-statable 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/ */

    

orobogenius / laravel-model-statable example snippets




use Orobogenius\Statable\Statable;

class User extends Model
{
    use Statable;
    //...
}




use Illuminate\Database\Eloquent\Model;
use Orobogenius\Statable\Statable;

class User extends Model
{
    use Statable;
    
    public function stateAdmin()
    {
        return [
            'is_admin' => true
        ];
    }
}

$user = User::find(1);
$user->states('admin');

// $user->is_admin: true

use Illuminate\Database\Eloquent\Model;
use Orobogenius\Statable\Statable;

class User extends Model
{
    use Statable;
    
    public function stateAdmin()
    {
        return [
            'is_admin' => true
        ];
    }
    
    public function stateModerator()
    {
        return [
            'is_moderator' => true
        ];
    }
}

$user = User::find(1);
$user->states(['admin', 'moderator']);

// $user->is_admin: true
// $user->is_moderator: true




use Illuminate\Database\Eloquent\Model;
use Orobogenius\Statable\Statable;

class User extends Model
{
    use Statable;
    
    public function stateSuperAdmin()
    {
        return [
            'is_super_admin' => function ($attributes) {
                return $this->is_admin && $this->is_moderator;
            }
        ];
    }
}




use Illuminate\Database\Eloquent\Model;
use Orobogenius\Statable\Statable;

class Invoice extends Model
{
    use Statable;
    
    public function items()
    {
        return $this->hasMany(InvoiceItem::class);
    }

    public function statePaid()
    {
        return [
            'status' => 'paid',
            'with_relations' => ['items' => 'processed']
        ];
    }
}




use Illuminate\Database\Eloquent\Model;
use Orobogenius\Statable\Statable;

class InvoiceItem extends Model
{
    use Statable;
    
    public function invoice()
    {
        return $this->belongsTo(Invoice::class);
    }

    public function stateProcessed()
    {
        return [
            'status' => 'processed'
        ];
    }
}

$invoice = Invoice::find(1);
$invoice->states('paid');

// $invoice->status: paid
// $invoice->items: (status) processed

    public function statePaid()
    {
        return [
            'status' => 'paid',
            'with_relations' => ['items' => ['processed', 'valid']]
        ];
    }