Git Product home page Git Product logo

public-id's Introduction

Public IDs in Laravel

This package automatically generates public ID for your Eloquent models.

What is a public ID and what are its advantages?

Most of your projects probably have the id auto-incremental field. There are many disadvantages of using that field to displaying resource (e.g. in routing: /posts/{id}). That's where public ID comes in place. Think of them like UUID but shorter and with no separator.

Installation

Step 1: Install the dependency

From the command line, run:

composer require crnkovic/public-id

This command will download and install the package into your vendor folder.

Step 2: Add service provider

Open up config/app.php and add the following line within your providers array:

Crnkovic\PublicID\PublicIDServiceProvider::class

This step will plug the package into your Laravel application.

Step 3: Publish the configuration file

From the command line, run:

php artisan vendor:publish --provider="Crnkovic\PublicID\PublicIDServiceProvider"

Usage

Adding the column to your database schema

Add the Crnkovic\PublicID\PublicID::column($table) line inside your Laravel migration. Example:

use Crnkovic\PublicID\PublicID;

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    PublicID::column($table);
});

Including the trait

Just include the Crnkovic\PublicID\HasPublicID trait inside of your Eloquent model, and that's it. Now, on every model creation your public ID will be added and on every update, no public ID will be changed.

use Crnkovic\PublicID\HasPublicID;

class Post extends Model
{
    use HasPublicID;
}

Configurating

You can configure the package to your own needs. The keys in the configuration file are as specified below.

Key Type Description Default value
key String Name of the column in the database env('PUBLIC_ID_KEY', 'public_id')
size Integer Size of the key 10
alphabet String Characters used for generating a key English alphabet

public-id's People

Contributors

crnkovic avatar

Watchers

V.Greshilov avatar

Forkers

ismareno

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.