Usage

Encrypted

{note} You're browsing the documentation for an old version of laravel-casters. Consider upgrading your project to v3. Check your version with the following command:

composer show rawilk/laravel-casters

With the Encrypted caster, you can encrypt and decrypt columns on your models automatically.

Mark any column on your model as encrypted:

<?php

namespace App\Models;

use Rawilk\LaravelCasters\Casts\Encrypted;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $casts = [
        'something_secret' => Encrypted::class,
    ];
}

You can work with the attribute as you normally would, but it will be encrypted in the database.

$user = new User;

$user->something_secret = 'secret info';
$user->save();

Encryption serializes the variable and decryption unserializes it, so you get out exactly what you put in. This usually means that no type casting is needed.

There may be cases however that you want everything casted to some type even if you put something else in. In those cases you can specify types (all of Eloquent's default casts are supported):

<?php

namespace App\Models;

use Rawilk\LaravelCasters\Casts\Encrypted;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $casts = [
        'an_integer' => Encrypted::class . ':integer',
        'a_string' => Encrypted::class . ':string',
        'decimal_with_two_places' => Encrypted::class . ':decimal:2',
    ];
}
Previous
Changelog
Caught a mistake? Suggest an edit on GitHub