All Projects → vkovic → Laravel Custom Casts

vkovic / Laravel Custom Casts

Licence: mit
Make your own custom cast type for Laravel model attributes

Projects that are alternatives of or similar to Laravel Custom Casts

Laravel Short Url
A Laravel package to shorten urls
Stars: ✭ 127 (-40.38%)
Mutual labels:  laravel, package
Nova Cashier Manager
Managing Stripe subscriptions inside the incredible Laravel Nova admin panel.
Stars: ✭ 150 (-29.58%)
Mutual labels:  laravel, package
Laravel Governor
Manage authorization with granular role-based permissions in your Laravel Apps.
Stars: ✭ 131 (-38.5%)
Mutual labels:  laravel, package
Laravel Optimus
Transform your internal id's to obfuscated integers based on Knuth's integer hash. Laravel wrapper for the Optimus Library by Jens Segers with multiple connections support.
Stars: ✭ 119 (-44.13%)
Mutual labels:  laravel, package
Laravel Feature
A package to manage feature flagging in a Laravel project.
Stars: ✭ 184 (-13.62%)
Mutual labels:  laravel, package
Guardian
Eloquent Guardian is a simple permissions system for your users. While there are many other packages for permissions, this one solves everything in the most eloquent way.
Stars: ✭ 121 (-43.19%)
Mutual labels:  laravel, package
Youtube
Upload a video to a single YouTube channel with Laravel 5.
Stars: ✭ 143 (-32.86%)
Mutual labels:  laravel, package
Flex Env
🌳 Manage your .env file in Laravel projects through artisan
Stars: ✭ 95 (-55.4%)
Mutual labels:  laravel, package
Laravel Messenger
Simple user messaging package for Laravel
Stars: ✭ 2,140 (+904.69%)
Mutual labels:  laravel, package
Roadrunner Laravel
RoadRunner ⇆ Laravel bridge
Stars: ✭ 167 (-21.6%)
Mutual labels:  laravel, package
Laravel Natural Language
This package makes using the Google Natural API in your laravel app a breeze with minimum to no configuration, clean syntax and a consistent package API.
Stars: ✭ 119 (-44.13%)
Mutual labels:  laravel, package
Skeleton Nova Tool
A skeleton repository for Spatie's Nova Packages
Stars: ✭ 191 (-10.33%)
Mutual labels:  laravel, package
Laravel Factory Prefill
Prefills factories with faker method suggestions to increase productivity
Stars: ✭ 104 (-51.17%)
Mutual labels:  laravel, package
Roadrunner Laravel
[ABANDONED] Moved to https://github.com/spiral/roadrunner-laravel
Stars: ✭ 124 (-41.78%)
Mutual labels:  laravel, package
Laravel Google Translate
This package makes using the Google Translate API in your laravel app a breeze with minimum to no configuration, clean syntax and a consistent package API.
Stars: ✭ 97 (-54.46%)
Mutual labels:  laravel, package
Laravel Paket
Composer GUI. Manage Laravel dependencies from web interface without switching to command line!
Stars: ✭ 143 (-32.86%)
Mutual labels:  laravel, package
Laravel Analytics
Analytics for the Laravel framework.
Stars: ✭ 91 (-57.28%)
Mutual labels:  laravel, package
Laravel Likeable
Rate Eloquent models with Likes and Dislikes in Laravel. Development moved to Laravel Love package!
Stars: ✭ 95 (-55.4%)
Mutual labels:  laravel, package
Laravel Location
A simple Laravel Package to sort Countries, States and Cities
Stars: ✭ 162 (-23.94%)
Mutual labels:  laravel, package
Nebula
Nebula is a minimalistic and easy to use administration tool for Laravel applications, made with Laravel, Alpine.js, and Tailwind CSS.
Stars: ✭ 190 (-10.8%)
Mutual labels:  laravel, package

Laravel Custom Casts

Build Downloads Stable License

Make your own cast type for Laravel model attributes

Laravel custom casts works similarly to Eloquent attribute casting, but with custom-defined logic (in a separate class). This means we can use the same casting logic across multiple models — we might write image upload logic and use it everywhere. In addition to casting to custom types, this package allows custom casts to listen and react to underlying model events.

Let's review some Laravel common cast types and examples of their usage:

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $casts = [
        'is_admin' => 'boolean',
        'login_count' => 'integer'
        'height' => 'decimal:2'
    ];
}

In addition to boolean, integer, and decimal, out of the box Laravel supports real, float, double, string, object, array, collection, date, datetime, and timestamp casts.

Sometimes it is convenient to handle more complex types with custom logic, and for casts to be able to listen and react to model events. This is where this package come in handy.

Handling events directly from custom casts can be very useful if, for example, we're storing an image using a custom casts and we need to delete it when the model is deleted. Check out the old documentation for this example.

📦 vkovic packages 📦

Please check out my other packages — they are all free, well-written, and some of them are useful 😄. If you find something interesting, consider giving me a hand with package development, suggesting an idea or some kind of improvement, starring the repo if you like it, or simply check out the code - there's a lot of useful stuff under the hood.

Compatibility

The package is compatible with Laravel versions 5.5, 5.6, 5.7, 5.8 and 6

and Lumen versions 5.5, 5.6, 5.7, 5.8.

Minimum supported version of PHP is 7.1. PHP 8 is also supported.

Installation

Install the package via Composer:

composer require vkovic/laravel-custom-casts

Usage

Utilizing a custom cast class

To enable custom casts in a model, use the HasCustomCasts trait and define which attributes will be casted using $casts - per Laravel standards.

// File: app/User.php

namespace App;

use App\CustomCasts\NameCast;
use Illuminate\Database\Eloquent\Model;
use Vkovic\LaravelCustomCasts\HasCustomCasts;

class User extends Model
{
    use HasCustomCasts;

    protected $casts = [
        'is_admin' => 'boolean', // <-- Laravel default cast type
        'name' => NameCast::class // <-- Our custom cast class (see the section below)
    ];
}

Defining a custom cast class

This class will be responsible for our custom casting logic.

// File: app/CustomCasts/NameCast.php

namespace App\CustomCasts;

use Vkovic\LaravelCustomCasts\CustomCastBase;

class NameCast extends CustomCastBase
{
    public function setAttribute($value)
    {
        return ucwords($value);
    }

    public function castAttribute($value)
    {
        return $this->getTitle() . ' ' . $value;
    }

    protected function getTitle()
    {
        return ['Mr.', 'Mrs.', 'Ms.', 'Miss'][rand(0, 3)];
    }
}

The required setAttribute method receives the $value being set on the model field, and should return a raw value to store in the database.

The optional castAttribute method receives the raw $value from the database, and should return a mutated value. If this method is omitted, the raw database value will be returned.

For the sake of this example we'll implement one more method which will attach a random title to a user when their name is retrieved from database.

Testing a custom cast class

Let's create a user and see what happens.

$user = new App\User;
$user->name = 'john doe';

$user->save();

This will create our new user and store their name in the database, with the first letter of each word uppercased.

When we retrieve the user and try to access their name, title will be prepended to it — just like we defined in our custom NameCast class.

dd($user->name); // 'Mr. John Doe'

Handling model events

Let's say that we want to notify our administrator when a user's name changes.

// File: app/CustomCasts/NameCast.php

public function updated()
{
    $attribute = $this->attribute;

    if($this->model->isDirty($attribute)) {
        // Notify admin about name change
    }
}

In addition to the updated method, we can define other methods for standard model events: retrieved, creating, created, updating, saving, saved, deleting, deleted, restoring and restored.

Other functionality

As you can see from the above code, we can easily access the casted attribute name as well as an instance of the underlying model.

// File: app/CustomCasts/NameCast.php

// Get the name of the model attribute being casted
dd($this->attribute); // 'name'

// Access our `User` model
dd(get_class($this->model)); // 'App/User'

We can also retrieve all casted attributes and their corresponding classes directly from the model.

// File: app/User.php

dd($this->getCustomCasts()); // ['name' => 'App/CustomCasts/NameCast']

Using aliased casts

You may find it easier to use aliases for custom casts, e.g.:

protected $casts = [
    'avatar' => 'image' // <-- You prefer this ...
    // ---
    'avatar' => ImageCast::class // <-- ... over this
];

To make the magic happen, first add the package's service provider to the providers array:

// File: config/app.php

'providers' => [
    // ...

    /*
     * Package Service Providers...
     */
    Vkovic\LaravelCustomCasts\CustomCastsServiceProvider::class

    // ...
]

Once the provider is added, publish the config file which will be used to associate aliases with their corresponding custom cast classes:

php artisan vendor:publish --provider="Vkovic\LaravelCustomCasts\CustomCastsServiceProvider"

This command should create a config file located at config/custom_casts.php. Open it up and check out the comments for examples of config options.

More examples

You can find more examples in the old documentation.

Contributing

If you plan to modify this Laravel package you should run the tests that come with it. The easiest way to accomplish this is with Docker, docker-compose, and phpunit.

First, initialize the Docker containers:

docker-compose up -d

Then you can run the tests and watch the output:

docker-compose exec app vendor/bin/phpunit
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].