All Projects β†’ matt-daneshvar β†’ eloquent-hashids

matt-daneshvar / eloquent-hashids

Licence: MIT license
Automatically generate and persist Hashids for newly created Eloquent models.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to eloquent-hashids

Eloquent Hashids
On-the-fly hashids for Laravel Eloquent models. (🍰 Easy & ⚑ Fast)
Stars: ✭ 196 (+1052.94%)
Mutual labels:  hashids, eloquent
laravel-hashid
HashId Implementation on Laravel Eloquent ORM
Stars: ✭ 23 (+35.29%)
Mutual labels:  hashids, eloquent
laratools
A collection of useful everyday tools for Laravel
Stars: ✭ 17 (+0%)
Mutual labels:  eloquent
state-machine
The hyn state machine package is a flexible library that helps you move Eloquent models from States through Transitions while emitting events along the way.
Stars: ✭ 14 (-17.65%)
Mutual labels:  eloquent
slim-boilerplate
A PHP boilerplate,for a fast API prototyping based on Slim Framework, for start projects with Eloquent ORM, Validation, Auth (JWT), Repositories and Transformers ready
Stars: ✭ 58 (+241.18%)
Mutual labels:  eloquent
inertiajs-tables-laravel-query-builder
Inertia.js Tables for Laravel Query Builder
Stars: ✭ 391 (+2200%)
Mutual labels:  eloquent
hashids-bundle
Integrates hashids/hashids in a Symfony project
Stars: ✭ 43 (+152.94%)
Mutual labels:  hashids
laravel-nestedupdater
Package for allowing updating of nested Eloquent model relations using a single nested data array.
Stars: ✭ 19 (+11.76%)
Mutual labels:  eloquent
php-orm-benchmark
The benchmark to compare performance of PHP ORM solutions.
Stars: ✭ 82 (+382.35%)
Mutual labels:  eloquent
hashids.sql
PL/pgSQL implementation of hashids library
Stars: ✭ 40 (+135.29%)
Mutual labels:  hashids
acorn-db
Provides Acorn projects with Eloquent Models for WordPress data.
Stars: ✭ 30 (+76.47%)
Mutual labels:  eloquent
plug
A collection of pluggable traits for Eloquent (Laravel) models
Stars: ✭ 13 (-23.53%)
Mutual labels:  eloquent
eloquent-filemaker
A Model extension and Eloquent driver for Laravel connecting to FileMaker through the Data API
Stars: ✭ 38 (+123.53%)
Mutual labels:  eloquent
laravel-query-inspector
The missing laravel helper that allows you to inspect your eloquent queries with it's bind parameters
Stars: ✭ 59 (+247.06%)
Mutual labels:  eloquent
laravel-quasar
β°πŸ“Šβœ¨Laravel Time Series - Provides an API to create and maintain data projections (statistics, aggregates, etc.) from your Eloquent models, and convert them to time series.
Stars: ✭ 78 (+358.82%)
Mutual labels:  eloquent
hashids.pm
Hashids, ported for Perl
Stars: ✭ 15 (-11.76%)
Mutual labels:  hashids
laravel-attribute-observer
Observe (and react to) attribute changes made on Eloquent models.
Stars: ✭ 59 (+247.06%)
Mutual labels:  eloquent
laravel-simplegrid
A simple component for generating powerful grids with Laravel.
Stars: ✭ 35 (+105.88%)
Mutual labels:  eloquent
encryptable
Laravel package for persisting encrypted Model properties, providing decryption when accessed.
Stars: ✭ 26 (+52.94%)
Mutual labels:  eloquent
laravel-sybase
Connection and Laravel Eloquent driver for Sybase
Stars: ✭ 29 (+70.59%)
Mutual labels:  eloquent

Eloquent Hashids for Laravel

Build Status GitHub

Automatically persist Hashids on your newly created Eloquent models using Ivan Akimov's Hashids library.

This can be useful when you need to generate unique alphanumeric (or any other character) combinations to represent your models.

Installation

Require the package using composer.

composer require matt-daneshvar/eloquent-hashids

Usage

Add a nullable hashid column to your database table in your migrations.

$table->string('hashid')->nullable();

Use the Hashid trait to automatically generate and persist Hashids for your new models. Optionally use HashidRouting to set your model to use the hashid column for Laravel's Route Model Binding.

use Illuminate\Database\Eloquent\Model;
use MattDaneshvar\EloquentHashids\Hashid;
use MattDaneshvar\EloquentHashids\HashidRouting;

class Receipt extends Model
{
    use Hashid, HashidRouting;
}

Customizing Hashid generation

While the package attempts to use sensible defaults to minimize configuration out of the box, you're free to adjust the Hashid generation behaviour using static properties on your model definition.

class Receipt extends Model
{
    use Hashid;
    
    /**
     * The column used to store Hashid.
     *
     * @var array
     */
    protected static $hashidColumn = 'hashid';
    
    /**
     * The minimum length of the generated Hashids.
     *
     * @var array
     */
    protected static $hashidMinLength = 8;
    
    /**
     * The whitelist of characters used inside the generated Hashids.
     *
     * @var array
     */
    protected static $hashidChars = 'abcdefghijklmnopqrstuvwxyz1234567890';
    
    /**
     * The salt for generating Hashids.
     *
     * @var array
     */
    protected static $hashidSalt = 'your unique salt';
    
    /**
     * The attribute encoded to generate the Hashid.
     *
     * @var array
     */
    protected static $hashidKey = 'id';
}

Changing the Hashid column

To customize the hashid column, set your own custom $hashidColumn value on your model.

class Receipt extends Model
{
    use Hashid;
    
    protected static $hashidColumn = 'uid';
}

Changing the salt

Each model's table name is by default used as the salt for generating Hashids. With that, models of separate classes that share the same IDs (e.g. a Task model with ID of 1 and a Receipt model also with ID of 1) would each have different Hashids. You may change this behaviour and override the salt by specifying the $hashidSlat on your model.

class Receipt extends Model
{
    use Hashid;
    
    protected static $hashidSalt = 'salt and pepper';
}

Creating your own Hashids instance

To fully customize the behaviour of the underlying Hashids library, you may also define your own Hashids instance in your model's boot method. Note that your Hashids instance would take precedence over all other customizations, and therefore all the rest of the static Hashid properties on your model (i.e. $hashidMinLength, $hashidChars, etc.) would be ignored once you specify your own Hashids instance.

class Receipt extends Model
{
    public static function boot()
    {
        parent::boot();
    
        static::$hashidsInstance = new Hashids('salt and pepper', 5);
    }
}

Using the HashidRouting trait

A common use case of Hashids with Eloquent models is to use short URLs using the generated Hashids as identifiers.

For example you may wish to represent your app's receipts using their Hashid values:

https://example.com/receipts/2ov7j3o3

instead of their IDs:

https://example.com/receipts/4

For more convenience this package comes with a HashidRouting trait out of the box; once added to your model, this trait will change the model's route key name to its corresponding Hashid column, which would allow you to take advantage of Laravel's Route Model Binding and use the Hashid URLs:

Route::get('api/receipts/{receipt}', function (App\Receipt $receipt) {
    return $receipt->total;
});

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].