2017 © Pedro Peláez
 

library laravel-eloquent-custom-casts

Provides Laravel Eloquent trait to enable custom class object casting.

image

slruslan/laravel-eloquent-custom-casts

Provides Laravel Eloquent trait to enable custom class object casting.

  • Saturday, May 20, 2017
  • by slruslan
  • Repository
  • 3 Watchers
  • 3 Stars
  • 57 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 10 % Grown

The README.md

Laravel Eloquent custom casts trait

Latest Version on Packagist License GPL, (*1)

Laravel Eloquent provides an Attribute Casting function, that allows you to automatically convert attributes to common data types. By default, supported cast types are: integer, real, float, double, string, boolean, object, array, collection, date, datetime, and timestamp., (*2)

As you can see, custom class types are not supported. This trait adds this support, so you automatically serialize and deserialize your custom classes., (*3)

Installation

Using Composer:, (*4)

``` bash $ composer require slruslan/laravel-eloquent-custom-casts --dev, (*5)


## Usage 1. Create a field of TEXT type where your data will be stored. 2. To enable trait, add ```use Slruslan\CustomCasts\CustomCasts;``` line to your Eloquent Model class. For example: ```php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Slruslan\CustomCasts\CustomCasts; class Post extends Model { use CustomCasts;
  1. Add your fields and class names to be serialized in protected $casts array.
    protected $casts = [
        'geo_location' => \App\Services\CustomLocation::class,
        'another_custom_field' => \App\Services\AnotherCustomField::class
    ];
  1. You're ready to use your model. Here is a basic example:
    $post = Post::find(1);

    // For example, imagine we have an \App\Services\CustomLocation class,
    // that implements any custom logics and for some reasons
    // we have to store it in DB as it is.

    // Let's instantinate it.
    $location = new \App\Services\CustomLocation(55.9937441, 92.7521816);

    // And call some methods, imagine we have them there.
    $location->updatePosition();
    $location->callAPI();

    // After that, we want to save it in our post model.
    // We can just assign the value and call default save() method.
    $post->geo_location = $location;
    $post->save();

    // It's saved. Let's get a post again
    // and check the field class.

    $post = Post::find(1);

    var_dump($post instanceof \App\Services\CustomLocation);

    // Outputs:
    // bool(true)

License

GNU General Public License v3.0 (GPL). Refer to the LICENSE file to get more info., (*6)

Author contacts:

You can ask me any questions by:, (*7)

Email: me@slinkov.xyz, (*8)

VK: vk.com/slruslan, (*9)

The Versions

20/05 2017

dev-master

9999999-dev https://github.com/slruslan/laravel-eloquent-custom-casts

Provides Laravel Eloquent trait to enable custom class object casting.

  Sources   Download

GPL

The Requires

  • php >=5.6

 

20/05 2017

1.0.2

1.0.2.0 https://github.com/slruslan/laravel-eloquent-custom-casts

Provides Laravel Eloquent trait to enable custom class object casting.

  Sources   Download

GPL

The Requires

  • php >=5.6

 

17/04 2017

1.0

1.0.0.0 https://github.com/slruslan/laravel-eloquent-custom-casts

Provides Laravel Eloquent trait to enable custom class object casting.

  Sources   Download

GPL

The Requires

  • php >=5.6