2017 © Pedro Peláez
 

library laravel-castable

Laravel package that adds custom casts for models attributes

image

gregoriohc/laravel-castable

Laravel package that adds custom casts for models attributes

  • Tuesday, April 10, 2018
  • by gregoriohc
  • Repository
  • 1 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

Laravel Castable

Latest Version on Packagist ![Software License][ico-license] Build Status ![Coverage Status][ico-scrutinizer] Quality Score ![Total Downloads][ico-downloads], (*1)

Laravel package that adds custom casts for models attributes., (*2)

Install

Via Composer, (*3)

``` bash $ composer require gregoriohc/laravel-castable, (*4)


In Laravel 5.5 the package will autoregister the service provider. In Laravel 5.4 and before you must install this service provider. ``` php // config/app.php 'providers' => [ ... Gregoriohc\Castable\ServiceProvider::class, ... ];

Usage

With Castable base model

The fastest way is using \Gregoriohc\Castable\CustomCastableModel as your base model., (*5)

Without Castable base model

Add HasCustomCasts trait to your the model you want to add custom attribute casting (if you want to use it in all your models, I suggest adding it to your own base model)., (*6)

Override castAttribute model method:, (*7)

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


Override `setAttribute` model method: ``` php public function setAttribute($key, $value) { return parent::setAttribute($key, $value)->customSetAttribute($key, $value); }

Override toArray model method:, (*9)

``` php public function toArray() { return $this->customToArray(parent::toArray()); }, (*10)


Add custom casted attributes to the model casts array: ``` php protected $casts = [ 'location' => 'point', 'bounding_box' => 'multipoint', ];

A full model example would look like this:, (*11)

``` php namespace App\Models;, (*12)

use Gregoriohc\Castable\HasCustomCasts;, (*13)

class Place extends \Illuminate\Database\Eloquent\Model { use HasCustomCasts;, (*14)

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());
}

}, (*15)


### Attribute migration, setting and getting Depending on the custom caster, the attribute will accept and return different values when setting/getting. Also, the required database migration type will differ. For the included casters, you can see the doc in the caster class file. For example, for the point caster requires a `Point` database migration type, and to set its value you can do the following: ``` php $place->location = [12.345, 67.890];

Configuration

You can optionally publish the config file with:, (*16)

``` bash $ php artisan vendor:publish --provider="Gregoriohc\Castable\ServiceProvider" --tag="config", (*17)


### Creating a custom caster You can create a custom caster extending the `\Gregoriohc\Castable\Casters\Caster` class and implementing `as` and `from` methods. For example: ``` 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); } }

The as method must transform the raw attribute value (from the database or internal) to the usable model attribute, and the from method must do the opposite thing., (*18)

After that, add the custom caster to the config file:, (*19)

``` php // config/castable.php 'casters' => [ ... 'serializable' => \App\Casters\SerializableObject::class, ... ];, (*20)


## Testing ``` bash $ composer test

Change log

Please see CHANGELOG for more information on what has changed recently., (*21)

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details., (*22)

Security

If you discover any security related issues, please email gregoriohc@gmail.com instead of using the issue tracker., (*23)

Socialware

You're free to use this package, but if it makes it to your production environment I highly appreciate you sharing it on any social network., (*24)

Credits

License

The MIT License (MIT). Please see License File for more information., (*25)

The Versions

10/04 2018

dev-master

9999999-dev https://github.com/gregoriohc/laravel-castable

Laravel package that adds custom casts for models attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel cast model attributes properties gregoriohc castable

10/04 2018

v2.0.3

2.0.3.0 https://github.com/gregoriohc/laravel-castable

Laravel package that adds custom casts for models attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel cast model attributes properties gregoriohc castable

10/04 2018

dev-develop

dev-develop https://github.com/gregoriohc/laravel-castable

Laravel package that adds custom casts for models attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel cast model attributes properties gregoriohc castable

10/04 2018
21/01 2018
20/01 2018

v1.0.2

1.0.2.0 https://github.com/gregoriohc/laravel-castable

Laravel package that adds custom casts for models attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel cast model attributes properties gregoriohc castable

08/01 2018

v1.0.1

1.0.1.0 https://github.com/gregoriohc/laravel-castable

Laravel package that adds custom casts for models attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel cast model package attributes properties

08/01 2018

v1.0.0

1.0.0.0 https://github.com/gregoriohc/laravel-castable

Laravel package that adds custom casts for models attributes

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel cast model package attributes properties