Skip to content

Instantly share code, notes, and snippets.

@axelitus
Last active September 17, 2018 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axelitus/d6222e649374aed03f2d06b41150de60 to your computer and use it in GitHub Desktop.
Save axelitus/d6222e649374aed03f2d06b41150de60 to your computer and use it in GitHub Desktop.
Laravel Database Encryption Use Case
<?php
// database/migrations/2018_09_17_155927_create_senders_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateSendersTable extends Migration
{
public function up()
{
Schema::create('senders', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('mail_config');
});
}
public function down()
{
Schema::dropIfExists('senders');
}
}
<?php
// app/Sender.php
namespace App;
use AustinHeap\Database\Encryption\Traits\HasEncryptedAttributes;
use Illuminate\Database\Eloquent\Model;
class Sender extends Model
{
use HasEncryptedAttributes;
protected $fillable =[
'name', 'mail_config',
];
protected $encrypted = [
'mail_config',
];
protected $casts = [
'mail_config' => 'array',
];
}
<?php
// database/factories/SenderFactory.php
use Faker\Generator as Faker;
$factory->define(App\Sender::class, function (Faker $faker) {
return [
//
];
});
<?php
// database/seeds/SendersTableSeeder.php
use App\Sender;
use Illuminate\Database\Seeder;
class SendersTableSeeder extends Seeder
{
public function run()
{
factory(Sender::class)->create([
'name' => 'Log Sender',
'mail_config' => [
'driver' => 'log',
'host' => null,
'port' => null,
'username' => null,
'password' => null,
'encryption' => null,
'from' => [
'address' => 'admin@localhost',
'name' => 'Local Admin',
]
],
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment