chistel/laravel-uniqueid

Generate uniqueid when saving Eloquent models to be used as auth key and route key

1.0.0 2019-10-13 01:12 UTC

This package is auto-updated.

Last update: 2024-04-23 12:05:35 UTC


README

but the difference is that it's meant to use unique string for model routes.

Installation

You can install the package via composer:

composer require chistel/laravel-uniqueid

Usage

Your Eloquent models should use the Chistel\LaravelUniqueId\HasUniqueId trait and the Chistel\LaravelUniqueId\UniqueIdOptions class.

The trait contains an abstract method getUniqueIdOptions() that you must implement yourself.

Here's an example of how to implement the trait:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Chistel\LaravelUniqueId\HasUniqueId;
use Chistel\LaravelUniqueId\UniqueIdOptions;

class UserModel extends Model
{
   use HasUniqueId;
    
   /**
	 * Get the route key for the model.
	 *
	 * @return string
	 */
	public function getRouteKeyName()
	{
	    return 'user_unique_id';  // this is the unique key column 
	}
	/**
	* Get the options for generating model uniqueId.
	*/
	public function getUniqueIdOptions() : UniqueIdOptions
	{
	  	return UniqueIdOptions::create()
	   	->saveUniqueIdTo('user_unique_id') // this is the unique key column 
	      	->uniqueIdShouldBeNoLongerThan(8);
	}
}

the uniqueid is only available on model creating.