Join 25,000+ Laravel Developers and join the free Laravel Newsletter
Laravel Database Hashing Package
News / November 05, 2018

Laravel Database Hashing Package

The Laravel Database Hashing package is a Laravel +5.5 package by Jack Noordhuis for automatically hashing database attributes. When enabled it automatically hashes data stored in model attributes.

It works by using a trait in combination with a class property:

<?php

use jacknoordhuis\database\hashing\traits\HasHashedAttributes;

class User extends Eloquent {
    use HasHashedAttributes;

    /**
     * The attributes that should be hashed on save.
     *
     * @var array
     */
    protected $hashing = [
        "username_lookup",
    ];
}

Here’s an example from the README that demonstrates using a password as a salt modifier so that the hash can only be re-created when the user provides their valid password:

$user->username_lookup = $user->hashAttribute($username, $password);

You can then find the given user with the following query, using the DatabaseHashing facade provided by this package:

User::where(
    "username_lookup",
    "=",
    \DatabaseHashing::create($request->get("username"), $request->get("password"))
);

I noticed that this package wasn’t ready for Laravel 5.7, which is the latest at the time of writing, but supports Laravel 5.5 and 5.6. If you want to help get this package ready for Laravel 5.7, you can learn about this package on GitHub at JackNoordhuis/laravel-database-hashing.

This appeared first on Laravel News
Laravel News Partners

Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.