Download the PHP package touhidurabir/laravel-model-hashid without Composer

On this page you can find all versions of the php package touhidurabir/laravel-model-hashid. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-model-hashid

Laravel Model Hashid

A package to generate model hash id from the model auto increment id for laravel models

Installation

Require the package using composer:

To publish the config file:

How it can be helpful?

When developing public APIs, sometimes we need to provide data based on givem model id from the requester endpoint . For example

where the give {id} can be model table associated auto increament id . Using an uuid is one another and now a days a pretty popular appraoch to obsecure the model table id. such as

But even with uuid, we do need to make adjustment to query to find model resource based on uuid or make the uuid the Primary Key of the model.

However this packaga take a different approach where on model resource creation , it gegerate and store an unique hash id from the model id and then use that to pass as response to remote request . And the requester use that hashid to make request to APIs instead of id or uuid but applying the middlewares, those hashid in the route param or request param dehashed to original model id . So basically this happens,

when making the api request, like this

Say that route url point to some controller method, then

Config Options

The published config file contails few possible configuration options . See and read through the config/hasher.php to get to know all the possible options . But few important ones are

enable

Determine if Hashid should be enabled or not . By default it is set to true.

key

Use this unique key as the base or salt to generate the hash . It is not an required details but it is highly recommened to use one unique key through out the app to make the hashid stronger .

column

The define which column by default this package should look for hashid to retrive or store the gererated one. But still possible to have some column name here and then have something different in some models.

alphabets

This defined the only characters that will be present in a hash string. Best To have along range of characters which by default is lower case a-z with upper case A-Z and 0-9.

NOTE : it must be at least 16 characters long and must only contains unique characters. no duplicate allowed like 'aaaabbbbbbcc' etc .

regeneration_job

This defined the job that will be used to run the update or fill up the missing hash id through the hashid:run command.

Command

This packaga includes a command that can use to set up the model hash id for the missing ones or update existing one . It will be helpful if this package included later in any laravel app that already have some data in it's tables and needed to set up hash id for those records. To utlize this command run

The one argument it reuired is the name of the models command seperated(if there is multiple models to run for) . Behind the scene it calls a queue job to go through the model recored and work on those to update/fill hash id column value. Other options as follow

--path=

By default it assumes all the models are located in the App\Models\ namespace . But if it's located some where else , use the option to define the proper model space path with trailing slash .

--update-all

By default this command will only work with those model records that have the defined hash id column null . So basically it will fill up the missing ones , but if this false is provided with the command it will update all regardless of hashid associated with or not.

--on-job

This defined if this will update/fill missing one through a queue job . The command use a job where the main logic resides in . But by default it uses the framework provided dispatchNow method to run the jon in sync way . if the falg provided and queue configured properly, it will push the job in the queue .

--job=

If need to pass custom queue job implementation, it can be directly provided though this option . also one can update the queue class in the config file .

Usage

Use the trait IdHashable in model where uuid needed to attach

By default this package use the column name hash_id to store the hash value of the model auto increment id.but this can be changed

Also possible to override the uuid column and attaching event from each of the model . to do that need to place the following method in the model :

Now to make hashed route or request params automatically, use this following 2 middlewares

Register these middlewares in Http\Kernel.php file or in controller/route file separately as require .

NOTE that the DehashRequestParams middleware can dehash request param that is String(simple hash string) or Array(array of hash string) only. So if require to handle complex param such as JSON string, need to handle that manually.

To handle such case where one need to decode some keys form a JSON response or for some other purpose need manual dehashing, this package provide 2 helper methods

As the name suggest, the decode_hashid can only work with a single hash where decode_hashids can work with single hash or array of hash.

Make sure to the put the hashid column name in migration file

Or can be used with the combination of hasher config as such:

This package also include some helper method that make it easy to find model records via UUID. for example

Or simple and direct find

The package also provide a bit of safe guard by checking if the model table has the given hash id column .

If the hash id column not found for model table schema, it will not create and attach an hash id.

Extras

For API resource

One big use case of this package for the development of API services where gthe developers do not want to include the model original auto incrementing id . For that case, one should use the hash_id as such

The above approach is perfectly fine. Howeever as the hash id is not just used mostly for any other model related purpose , one may want to make it hidden like

For such case, this package includes a simple trait to use with the api resource classes,

Using the core Hasher class

The core of this package is the Hasher class that handle the whole hash decode/encode process. This is hightly dependent on the popular php Hashid Package with some bit of extra functionality . One can also use this hasher as for their need fits . To see the details of the hasher class and how it works, check the code itself at Touhidurabir\ModelHashid\Hasher\Hasher .

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT


All versions of laravel-model-hashid with dependencies

PHP Build Version
Package Version
Requires php Version >=7.3
hashids/hashids Version ^4.1
illuminate/bus Version ^8.61
illuminate/container Version ^8.55
illuminate/contracts Version ^8.61
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package touhidurabir/laravel-model-hashid contains the following files

Loading the files please wait ....