PHP code example of gregoriohc / laravel-castable

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

    

gregoriohc / laravel-castable example snippets

 php
// config/app.php
'providers' => [
    ...
    Gregoriohc\Castable\ServiceProvider::class,
    ...
];
 php
protected function castAttribute($key, $value)
{
    return $this->customCastAttribute($key, parent::castAttribute($key, $value));
}
 php
public function setAttribute($key, $value)
{
    return parent::setAttribute($key, $value)->customSetAttribute($key, $value);
}
 php
public function toArray()
{
    return $this->customToArray(parent::toArray());
}
 php
protected $casts = [
    'location' => 'point',
    'bounding_box' => 'multipoint',
];
 php
namespace App\Models;

use Gregoriohc\Castable\HasCustomCasts;

class Place extends \Illuminate\Database\Eloquent\Model
{
    use HasCustomCasts;
    
    protected $casts = [
        'location' => 'point',
        'bounding_box' => 'multipoint',
    ];

    protected function castAttribute($key, $value)
    {
        return $this->customCastAttribute($key, parent::castAttribute($key, $value));
    }

    public function setAttribute($key, $value)
    {
        return parent::setAttribute($key, $value)->customSetAttribute($key, $value);
    }
    
    public function toArray()
    {
        return $this->customToArray(parent::toArray());
    }
}
 php
$place->location = [12.345, 67.890];
 php
namespace \App\Casters;

class SerializableObject extends \Gregoriohc\Castable\Casters\Caster
{
    public function as($value)
    {
        return unserialize($value);
    }

    public function from($value)
    {
        return serialize($value);
    }
}
 php
// config/castable.php
'casters' => [
    ...
    'serializable' => \App\Casters\SerializableObject::class,
    ...
];