PHP code example of igaster / eloquent-decorator

1. Go to this page and download the library: Download igaster/eloquent-decorator 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/ */

    

igaster / eloquent-decorator example snippets


use igaster\EloquentDecorator\EloquentDecoratorTrait;

use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Contracts\Queue\QueueableEntity;
use ArrayAccess;
use JsonSerializable;

class SuperUser implements ArrayAccess, Arrayable, Jsonable, JsonSerializable, QueueableEntity, UrlRoutable
{
    use EloquentDecoratorTrait;

    public $newProperty;		// Add a new property
    public $overridenPropery;	// or Overide a property (it hides it)

    public function TruncateDatabase(){
    	// add your functions...
    }
}

	$user = User::find(1);

	$superUser = SuperUser::wrap($user);

	// you can think the SuperUser class as inhereted from the User model. so you can still do:

	$superUser->name = "Admin";
	$superUser->save();

	// But you also have access to SuperUser functions/attributes:

	$superUser->TruncateDatabase();

	// You can always access to the original (decorated object):
	$superUser->object->someProperty;
}