brainstud/has-identifier
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/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 | | ||
| v2.1.0 | ^7.4 | | ^8.0 | | ||
| v2.0.6 | ^7.4 | | ^8.0 | | ||
| v2.0.5 | ^7.4 | | ^8.0 | | ||
| v2.0.4 | ^7.4 | | ^8.0| | ||
| v2.0.3 | ^7.4 | | ^8.0| | MIT | |
| v2.0.2 | ^7.4 | | ^8.0| | ||
| v2.0.1 | ^7.4 | | ^8.0| | ||
| v2.0.0 | ^7.4 | | ^8.0 | ||
| v1.0.3 | ^7.4 | | ^8.2 | ||
| v1.0.2 | ^8.0 | ^8.2 | ||
| v1.0.1 | ^8.0 | ^8.2 | ||
| v1.0.0 | ^8.0 | ^8.2 |