Laravel Database Hashing Package

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.


Filed in: Laravel Packages


Newsletter

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

Laravel News Partners

Laravel Jobs

Full-Stack Developer
Tampa, FL / Remote
Nu Image Medical
PHP Developer
Remote
X-Team
Full-Stack Developer
Sioux Falls, SD or Remote
Electric Pulp
Senior PHP / Vue.js Developer
Portland, OR / Remote
Bright Oak
Full Stack Vue.js Developer (Front-end focus)
Remote
LenderHomePage.com
Senior Laravel Web Application Developer
NYC or Remote
Happy Cog
Laravel Developer (Vue, SQL Server)
Geneva, Switzerland / Remote
World Trade Organization