PHP code example of mindtwo / laravel-decorator

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

    

mindtwo / laravel-decorator example snippets


use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelDecorator\Interfaces\Decoratable;
use mindtwo\LaravelDecorator\Traits\HasDecorator;

class MyModel extends Model implements Decoratable
{
    use HasDecorator;
}

use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelDecorator\Interfaces\Decoratable;
use mindtwo\LaravelDecorator\Traits\HasDecorator;

class MyModel extends Model implements Decoratable
{
    use HasDecorator;

    /**
     * Return the default decorator full qualified class name.
     *
     * @return string
     */
    public function defaultDecorator(): string
    {
        return MyDecorator::class;
    }
}

use mindtwo\LaravelDecorator\Decorator;

class MyDecorator extends Decorator
{
    /**
     * Get formatted creation date.
     *
     * @return string
     */
    public function defaultDecorator(): string
    {
        return $this->model->created_at->format('Y-m-d');
    }
}

$myObject = MyModel::make();

// Use a certain decorator
$myDecoratedObject = $myObject->decorate(MyDecorator::class);

// Use the default decorator (needs to be defined on the model)
$myDecoratedObject = $myObject->decorate();

$myCollection = MyModel::get();

// Use a certain decorator
$myDecoratedCollection = $myCollection->decorate(MyDecorator::class);

// Use the default decorator (needs to be defined on the model)
$myDecoratedCollection = $myCollection->decorate();