brainstud/has-identifier

Add a HasIdentifier to the model to take care of generating the identifier
6,171
Install
composer require brainstud/has-identifier
Latest Version:v3.0.0
PHP:^8.3
Last Updated:Apr 16, 2026
Links: GitHub  ·  Packagist
Maintainer: Diondb

Has Identifier

Automatically fill the identifier of your model by adding the HasIdentifier trait.

Installation

Require the package

composer require brainstud/has-identifier

Usage

Add the HasIdentifier trait to your model.

/**
 * @property string $identifier
 */
class Item extends Model
{
    use HasIdentifier;
    
    protected $fillable = [
        'identifier'
    ];
}

The identifier will be filled on creation.

$model = Item::create();
echo $model->identifier;

Use a different identifier attribute

The trait will use the identifier property of your model to generate an identifier to. You can overwrite the property the trait uses by setting the identifierAttribute on your model.

/**
 * @property string $identifier
 */
class Item extends Model
{
    use HasIdentifier;
    
    protected string $identifierAttribute = 'uuid';
    
    protected $fillable = [
        'uuid'
    ];
}

Find by identifier

There are different ways to get the model by identifier.

// Find method
$model = Item::findByIdentifier($uuid);
$model = Item::findOrFailByIdentifier($uuid);

// Scope
$model = Item::identifiedBy($uuid)->first();

Test

You can run the tests with

composer test

License

has-identifier is open-sourced software licensed under the MIT License

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

637,827,751 9,703
laravel/framework

The Laravel Framework.

589,172,971 34,950
laravel/tinker

Powerful REPL for the Laravel framework.

498,216,183 7,442
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

413,412,504 609
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

394,964,158 4,660

Version History

Version Released PHP Laravel License
v3.0.0 ^8.3 ^12.0 || ^13.0
v2.1.0 ^7.4 || ^8.2 ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0
v2.0.6 ^7.4 || ^8.2 ^8.0 || ^9.0 || ^10.0 || ^11.0
v2.0.5 ^7.4 || ^8.2 ^8.0 || ^9.0 || ^10.0
v2.0.4 ^7.4 || ^8.0 ^8.0|^9|^10