PHPackages                             macmotp/codegen-laravel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. macmotp/codegen-laravel

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

macmotp/codegen-laravel
=======================

Generate human friendly codes - Laravel Extension

v0.3.0(2y ago)344MITPHPPHP &gt;=8.1

Since May 17Pushed 2y ago1 watchersCompare

[ Source](https://github.com/macmotp/codegen-laravel)[ Packagist](https://packagist.org/packages/macmotp/codegen-laravel)[ Docs](https://github.com/macmotp/codegen-laravel)[ RSS](/packages/macmotp-codegen-laravel/feed)WikiDiscussions main Synced today

READMEChangelog (7)Dependencies (4)Versions (15)Used By (0)

Code Generator - Laravel Extension
==================================

[](#code-generator---laravel-extension)

[![Latest Version on Packagist](https://camo.githubusercontent.com/ba7c707776c70761af6919684d608a0a401420f9df78a264a933f4675fd027bb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d61636d6f74702f636f646567656e2d6c61726176656c2e737667)](https://packagist.org/packages/macmotp/codegen-laravel)[![Total Downloads](https://camo.githubusercontent.com/526c164c2c58da22d943bd8fc97b49d7d4c212b265d82babdd6e87cdca4a650c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d61636d6f74702f636f646567656e2d6c61726176656c2e737667)](https://packagist.org/packages/macmotp/codegen-laravel)

**Generate human friendly codes**

Useful for generation of referral codes based on names, receipt numbers, unique references.

#### This is the Laravel extension for the package [Codegen - Generate Human Friendly Codes](https://github.com/macmotp/codegen)

[](#this-is-the-laravel-extension-for-the-package-codegen---generate-human-friendly-codes)

Requirements
------------

[](#requirements)

- PHP &gt;=8.1
- Laravel &gt;= 5

*if you use PHP 7.4, please make sure to use v0.1.2*

*if you use PHP 8.0, please make sure to use v0.2.0*

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require macmotp/codegen-laravel
```

Usage
-----

[](#usage)

#### Create semantic and sanitized reference codes from any model by applying the trait

[](#create-semantic-and-sanitized-reference-codes-from-any-model-by-applying-the-trait)

```
use Illuminate\Database\Eloquent\Model;
use Macmotp\HasCodegen;

class User extends Model
{
    use HasCodegen;

    protected $fillable = [
        'name',
        'code',
    ];

    /**
     * Attribute of the model used to generate the code
     *
     * @return string
     */
    protected function buildCodeFrom(): string
    {
        return $this->name;
    }
}
```

#### On the `creating` event, it will generate a human readable code to the specified column

[](#on-the-creating-event-it-will-generate-a-human-readable-code-to-the-specified-column)

```
$user = User::create([
    'name' => 'Bob McLovin',
]);

dump($user->code);
// (string) 'BBMCLV';
```

Configuration
-------------

[](#configuration)

#### Publish default configuration

[](#publish-default-configuration)

Create `config/codegen.php` file, where you can adjust a few settings:

```
