All Projects β†’ Weebly β†’ laravel-mutate

Weebly / laravel-mutate

Licence: BSD-2-Clause License
Mutate Laravel attributes

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-mutate

attribute-events
πŸ”₯ Fire events on attribute changes of your Eloquent model
Stars: ✭ 198 (+1423.08%)
Mutual labels:  eloquent, accessors
Laravel Graphql
GraphQL implementation with power of Laravel
Stars: ✭ 56 (+330.77%)
Mutual labels:  eloquent, transformer
zero-administration-inference-with-aws-lambda-for-hugging-face
Zero administration inference with AWS Lambda for πŸ€—
Stars: ✭ 19 (+46.15%)
Mutual labels:  transformer
attention-is-all-you-need-paper
Implementation of Vaswani, Ashish, et al. "Attention is all you need." Advances in neural information processing systems. 2017.
Stars: ✭ 97 (+646.15%)
Mutual labels:  transformer
laravel5-jsonapi-dingo
Laravel5 JSONAPI and Dingo together to build APIs fast
Stars: ✭ 29 (+123.08%)
Mutual labels:  transformer
kosr
Korean speech recognition based on transformer (트랜슀포머 기반 ν•œκ΅­μ–΄ μŒμ„± 인식)
Stars: ✭ 25 (+92.31%)
Mutual labels:  transformer
semantic-document-relations
Implementation, trained models and result data for the paper "Pairwise Multi-Class Document Classification for Semantic Relations between Wikipedia Articles"
Stars: ✭ 21 (+61.54%)
Mutual labels:  transformer
laravel-geoly
Perform fast and efficient radius searches on your Laravel Eloquent models.
Stars: ✭ 25 (+92.31%)
Mutual labels:  eloquent
laravel-autonumber
Laravel package to create autonumber for Eloquent model
Stars: ✭ 26 (+100%)
Mutual labels:  eloquent
laravel-json-syncer
A Json importer and exporter for Laravel.
Stars: ✭ 22 (+69.23%)
Mutual labels:  eloquent
trapper
State-of-the-art NLP through transformer models in a modular design and consistent APIs.
Stars: ✭ 28 (+115.38%)
Mutual labels:  transformer
are-16-heads-really-better-than-1
Code for the paper "Are Sixteen Heads Really Better than One?"
Stars: ✭ 128 (+884.62%)
Mutual labels:  transformer
Visual-Transformer-Paper-Summary
Summary of Transformer applications for computer vision tasks.
Stars: ✭ 51 (+292.31%)
Mutual labels:  transformer
pynmt
a simple and complete pytorch implementation of neural machine translation system
Stars: ✭ 13 (+0%)
Mutual labels:  transformer
SOLQ
"SOLQ: Segmenting Objects by Learning Queries", SOLQ is an end-to-end instance segmentation framework with Transformer.
Stars: ✭ 159 (+1123.08%)
Mutual labels:  transformer
Filipino-Text-Benchmarks
Open-source benchmark datasets and pretrained transformer models in the Filipino language.
Stars: ✭ 22 (+69.23%)
Mutual labels:  transformer
eloquent-traits
Traits for laravel eloquent models
Stars: ✭ 18 (+38.46%)
Mutual labels:  eloquent
laravel-boolean-dates
Automatically convert Eloquent model boolean attributes to dates (and back).
Stars: ✭ 31 (+138.46%)
Mutual labels:  eloquent
PAML
Personalizing Dialogue Agents via Meta-Learning
Stars: ✭ 114 (+776.92%)
Mutual labels:  transformer
restql
πŸ“¦ A data resolution package for your Laravel models.
Stars: ✭ 132 (+915.38%)
Mutual labels:  eloquent

Eloquent Mutators

License Latest Stable Version StyleCI Tests

This package allows you to map your model attributes to database columns when the type in the PHP model does not match the type in the database column.

This could be using $model->ip_address as a string in your eloquent model but storing it as a BINARY(16) in the database for efficiency. Or having a string always encrypted in the DB but readable in clear form within your models.

Installing

$ composer require weebly/laravel-mutate

To use this package, you'll need to add the ServiceProvider to the providers array in config/app.php if you are not using automatic package discovery:

Weebly\Mutate\LaravelMutatorServiceProvider::class

You'll also need to publish the config to config/mutators.php with:

$ php artisan vendor:publish --provider='Weebly\Mutate\LaravelMutatorServiceProvider'

Usage

When creating an Eloquent model, you'll need to extend Weebly\Mutate\Database\Model and add $mutate property it:

<?php

namespace App\Models;

use Weebly\Mutate\Database\Model;

class User extends Model
{
    /**
     * {@inheritdoc}
     */
    protected $table = 'users';

    /**
     * {@inheritdoc}
     */
    protected $mutate = [
        'id' => 'uuid_v1_binary'
    ];
}

This will automatically serialize/unserialize the id attribute on the User model when getting/setting the attribute from the database. This allows you to no longer need to set accessors/mutator methods on the model directly.

Note: Unlike the built in Laravel accessors/mutators, this package will serialize the attribute values when they are passed to an Eloquent query builder.

Included Mutators

  • uuid_v1_binary Will take a uuid version 1, re-order its bytes so that if uuidA was generated before uuidB, then storedUuidA < storedUuidB, and store it in the database as 16 bytes of data. For more information on the re-ordering of bytes, see: https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/.
  • ip_binary Will take a string representation of an IPv4 or IPv6 and store it as 16 bytes in the database.
  • encrypt_string Will take a non encrypted string and encrypt it when going to the database.
  • hex_binary Will take any hexadecimal string attribute and store it as binary data.
  • unix_timestamp Will take a Carbon date but store it as an integer unix timestamp.

Creating Custom Mutators

To define a custom mutator, you'll need to create a class that implements Weebly\Mutate\Mutators\MutatorContract, and add it to the enabled array in config/mutators.php.

Note: All attributes are cached on a model instance automatically, so you should not need to add any caching logic at the mutator level.

When building and registering a Mutator, it is important to know that they are resolved automatically from the Laravel IOC container, which means you may create service providers for them if they require custom constructor arguments.

<?php

namespace App\Mutators;

use Weebly\Mutate\Mutators\MutatorContract;

class ExampleEncryptMutator implements MutatorContract
{
    /**
     * {@inheritdoc}
     */
    public function serializeAttribute($value)
    {
        return encrypt($value);
    }

    /**
     * {@inheritdoc}
     */
    public function unserializeAttribute($value)
    {
        return decrypt($value);
    }
}

Testing

Running tests:

$ ./vendor/bin/phpunit

License

This package is open-sourced software licensed under the 2-Clause BSD license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].