2017 © Pedro PelĆ”ez
 

library eloquent-uuid

Adds support for UUID generation automatically for Models Eloquents.

image

your-app-rocks/eloquent-uuid

Adds support for UUID generation automatically for Models Eloquents.

  • Thursday, May 31, 2018
  • by joaorobertopb
  • Repository
  • 3 Watchers
  • 20 Stars
  • 162 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 18 % Grown

The README.md

YourApp.Rocks logo , (*1)

Eloquent UUID

Software License Build Status Latest Stable Version Total Downloads, (*2)

Simple and flexible Laravel package that adds support for UUID generation automatically for any Eloquent model., (*3)

  • Generate uuid automatically.
  • Choose a custom name for the uuid column in your table. (default 'uuid')
  • Choose the version of the generated uuid. (default '4')
  • Checks for uuid version and column name. (throws the InvalidUuidVersionException and MissingUuidColumnException exceptions)
  • Prevents update on uuid value.

What is a UUID?

A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems. is a 36 character long identifier made up of 32 alphanumeric characters with four hyphens in amongst it. For example:123E4567-E89b-12D3-A456-426655440000 containing letters and numbers. that will uniquely identify something. you can read more here., (*4)

Installation

You can install the package via Composer:, (*5)

``` bash composer require your-app-rocks/eloquent-uuid, (*6)


or via `composer.json` file ```json { "require": { "your-app-rocks/eloquent-uuid": "~2" } }

Usage

Create table

Create your table with a uuid column. For example:, (*7)

<?php

Schema::create('users', function (Blueprint $table) {
    $table->uuid('uuid');
    $table->string('name');
    $table->timestamps();
});

Create model

In your eloquent model, add trait HasUuid:, (*8)

<?php

namespace App\YourNameSpace;

use Illuminate\Database\Eloquent\Model;
use YourAppRocks\EloquentUuid\Traits\HasUuid;

class User extends Model
{
    use HasUuid;
}

Create Controller

<?php

namespace App\YourNameSpace;

use App\YourNameSpace\User;
use Illuminate\Http\Request;

class UserController extends Controller
{

    /**
     * When a new record is inserted into the table `(with the create() or save() methods)`,
     * Trait "HasUuid" will automatically generate a uuid version 4* for the 'uuid' column of your schema.
     */
    public function store(Request $request)
    {
        $user = User::create($request->all()); // Automatically generate a uuid

        return $user->getUuid() // Return UUID value.
    }

    /**
     * Get User by custom 'UUID' key name - Implicit Binding.
     * See https://laravel.com/docs/5.8/routing#route-model-binding
     *
     * @param User $user
     * @return void
     */
    public function show(User $user)
    {
        return $user;
    }

    //OR

    /**
     * Get User by scope query.
     */
    public function show($uuid)
    {
        $user = User::findByUuid($uuid);

        return $user;
    }
}

Customization

You can easily config the package for your needs by changing the column name and uuid version. For example:, (*9)

<?php

//Create table
Schema::create('posts', function (Blueprint $table) {
    $table->uuid('universally_unique_id');
    $table->string('title');
    $table->timestamps();
});

//Eloquent Model
class Post extends Model
{
    use HasUuid;

    protected $uuidColumnName = 'universally_unique_id';
    protected $uuidVersion = 1;    // Available 1,3,4 or 5
    protected $uuidString  = '';   // Needed when $uuidVersion is "3 or 5"
}

Advance Customization

This package was built to be flexible and easy to customize!, (*10)

You can use trait Uuidable to create your own trait with your custom code., (*11)

Methods

YourAppRocks\EloquentUuid\Traits\Uuidable;

  • getUuidColumnName() // Get the column name. ( default 'uuid' )
  • getUuid() // Get the uuid value.
  • setUuid($value) // Set the uuid value.
  • generateUuid() // Generate the UUID value. ( Using Ramsey\Uuid )
  • getUuidVersion() // Get uuid version or default to 4.
  • getUuidString() // Get string to generate uuid version 3 and 5.
  • validateUuidVersion() // Validate uuid version.

Example custom code

Replacing trait HasUuid for MyUuidTrait:, (*12)

<?php

//Create table
Schema::create('users', function (Blueprint $table) {
    $table->uuid('uuid');
    $table->string('name');
    $table->timestamps();
});

//Create MyUuidTrait with custom code
use YourAppRocks\EloquentUuid\Traits\Uuidable;

trait MyUuidTrait
{
    use Uuidable;

    /**
     * Boot trait on the model.
     *
     * @return void
     */
    public static function bootMyUuidTrait()
    {
        static::creating(function ($model) {
            // My custom code here.
        });

        static::saving(function ($model) {
            // My custom code here.
        });
    }

    // My custom code here.
}

//Create Model
use MyUuidTrait;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use MyUuidTrait;
}

Change log

Please see CHANGELOG for more information on what has changed recently., (*13)

Testing

bash $ composer test, (*14)

Contributing

Please see CONTRIBUTING for details., (*15)

Credits

This package is inspired by this package., (*16)

License

The MIT License (MIT). Please see License File for more information., (*17)

The Versions

31/05 2018

dev-master

9999999-dev http://yourapp.rocks

Adds support for UUID generation automatically for Models Eloquents.

  Sources   Download

MIT

The Requires

 

The Development Requires

by João Roberto P. Borges

uuid eloquent-uuid yourapp.rocks universal unique id

31/05 2018

1.2.1

1.2.1.0 http://yourapp.rocks

Adds support for UUID generation automatically for Models Eloquents.

  Sources   Download

MIT

The Requires

 

The Development Requires

by João Roberto P. Borges

uuid eloquent-uuid yourapp.rocks universal unique id

30/04 2018

1.2.0

1.2.0.0 http://yourapp.rocks

Adds support for UUID generation automatically for Models Eloquents.

  Sources   Download

MIT

The Requires

 

The Development Requires

by João Roberto P. Borges

uuid eloquent-uuid yourapp.rocks universal unique id

29/04 2018

1.1.0

1.1.0.0 http://yourapp.rocks

Adds support for UUID generation automatically for Models Eloquents.

  Sources   Download

MIT

The Requires

 

The Development Requires

by João Roberto P. Borges

uuid eloquent-uuid yourapp.rocks universal unique id

25/03 2018

1.0.0

1.0.0.0 http://yourapp.rocks

Adds support for UUID generation automatically for Models Eloquents.

  Sources   Download

MIT

The Requires

 

The Development Requires

by João Roberto P. Borges

uuid eloquent-uuid yourapp.rocks universal unique id