GithubHelp home page GithubHelp logo

thandonguocmo2020 / eloquent-uuid Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alsofronie/eloquent-uuid

0.0 2.0 0.0 51 KB

An Eloquent UUID Trait to use with Laravel 5.1 and 5.2

License: MIT License

PHP 100.00%

eloquent-uuid's Introduction

eloquent-uuid

An Eloquent UUID Trait to use with Laravel 5.1, 5.2 and 5.3.

MIT licensed

It should work with Laravel 5.0 also, but it's untested.

The trait overwrites the static boot method and listens to the creating event. It generates a UUID (strips the dashes) and stores it in the primary key attribute. Thus, you'll need a CHAR(32) primary key for your model (see migrations below).

Installation

composer require alsofronie/eloquent-uuid:dev-master

Use

In order to make it faster, you have the option to use one of three traits:

  • UuidModelTrait - the key must be CHAR(36) and contains the dashes
  • Uuid32ModelTrait - the key must be CHAR(32), the dashes are striped
  • UuidBinaryModelTrait - the key is BINARY(16).

Using UuidModelTrait

In order to use this trait, your schema must be something like:

<?php
	// ...
	Schema::create('users', function (Blueprint $table) {
		$table->uuid('id');	// this will create a CHAR(36) field
		// or
		// $table->char('id', 36);
		$table->string('username', 32);
		$table->string('password', 50);
		// ...
		$table->primary('id');
	});

Using Uuid32ModelTrait

For this type, just use CHAR(32) in your schema (this is identical to the first one, but with stripped dashes).

<?php
	// ...
	Schema::create('users', function (Blueprint $table) {
		$table->char('id', 32);
		// ...
		$table->string('username', 32);
		$table->string('password', 50);

		$table->primary('id');
	});

Using UuidBinaryModelTrait

This stores the key as binary. The default Laravel Blueprint curretly does not currently support binary fields with specified length, and (at least in MySQL) you cannot create an index (including primary key) on a BINARY field without length.

So, the schema definition should be something like this (please double check if you're not using MySQL):

<?php

	// ...
	Schema::create('users', function (Blueprint $table) {
		$table->string('username', 32);
		$table->string('password', 50);
	});

	DB::statement('ALTER TABLE `usersb` ADD `id` BINARY(16); ALTER TABLE `usersb` ADD PRIMARY KEY (`id`);')
?>

There are two additional notes for this particular trait.

Note 1. In order to get a string representation of your uuid, simple call $model->id_string and you'll get it.

Note 2. You can use User::find($uuid) with both the binary version or the string (bin2hex) version.

Using the optimized uuid

To use the optimized uuid, put the following line in your models: private static $uuidOptimization = true;

In your models

In order to use this in your models, just put use Uuid[32|Binary]ModelTrait;:

<?php

namespace App;
use Alsofronie\Uuid\Uuid[32|Binary]ModelTrait;

class User extends Eloquent
{
	use Uuid[32|Binary]ModelTrait;
}

Running tests

To run the tests, just run composer install and ./vendor/bin/phpunit.

eloquent-uuid's People

Contributors

alsofronie avatar jvalck avatar silasrm avatar dannyweeks avatar rawaludin avatar zmorris avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.