Code Monkey home page Code Monkey logo

smart-hash's Introduction

Laravel Eloquent Models Smart-HashIDs

What is the hashids?

Hashids is a small open-source library that generates short, unique, non-sequential ids from numbers. It converts numbers like 347 into strings like “yr8”, or array of numbers like [27, 986] into “3kTMd”.

For more information visit hashids.org.

Why we should use this library?

Your client deals with hash id, but each time your app received id or ids in the request SmartHash will decode it for you.

  • Easy to use!
  • Hidden your database ids from client.
  • Automatic encode model id for front-end and decode for back-end (Decode all id or ids requests from parameters or the body)
  • Does not need to methods like findByHashid

Installation

$ composer require mvaliolahi/smart-hash

$ php artisan vendor:publish

How to use

  1. inside your Model use SmartHash trait.
class Category extends Model
{
    use SmartHash;
}

$category->hashId()

After use SmartHash Trait, Category::first()->id will return jR not 1, You can access to original id using the $category->id() method.

  1. Inside you boot method in AppServiceProvider define your models because of route model binding:
$this->app->singleton('smart-hash', function() {
    return [
        'category' => Category::class,
        'user'     => User::class,
    ];
});

Add a new parameter to decoder lookup-table

edit config/smart-hash.php file, next step is obvious! for example, if we want to auto decode parameters like id, category_id and array parameter like ids, config is like below.

<?php

return [
    'single_parameters' => [
        'id', 
        'category_id'
    ],
    'array_parameters' => [
        'ids',
    ]
];

Manual find

$post = Post::findByHash('vm');
$post = Post::findOrFailByHash('vm');

smart-hash's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.