1. Go to this page and download the library: Download timyouri/laravel-customid library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
timyouri / laravel-customid example snippets
namespace App;
use Illuminate\Database\Eloquent\Model;
use TimYouri\CustomId\Traits\GeneratesCustomId;
class User extends Model
{
use GeneratesCustomId;
protected $keyType = 'string';
}
namespace App;
use Illuminate\Database\Eloquent\Model;
use TimYouri\CustomId\Traits\GeneratesCustomId;
class User extends Model
{
use GeneratesCustomId;
// This is a default Laravel prop.
// Required if using a string as ID.
protected $keyType = 'string';
// Determines if the generated id should be unique.
protected $uniqueCustomId = true;
// If `$uniqueCustomId` is true, this prop defines the maximum amount of attemts for validating uniqueness before inserting.
// When exceeded, it will throw an exception.
protected $customIdAttempts = 10;
// Determines if you are allowed to change the id. If true it will revert to the previous value when trying to update the id.
protected $lockCustomId = true;
// Config parameter for defining the length of the id in the default id generation method.
protected $customIdLength = 12;
// Overwrite the default id generation method
protected function generateId(int $attempts)
{
// `$attempts` is passed to write your own logic based on tried attempts. The default method does not make use of this param.
// Default:
return (string) Str::random($this->customIdLength);
// Example random number:
return (int) random_int(100000, 999999);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.