waska14/laravel-uuid 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

waska14/laravel-uuid

Composer 安装命令:

composer require waska14/laravel-uuid

包简介

Use uuid in models easily

README 文档

README

If you want to use uuid columns and fill them automatically, this package is for you.

This is a Laravel package for using (auto-generating) uuid in models. With this package you write less code, as the uuid(s) are being created, when you first save your instances.

Docs

Laravel compatibility

Laravel Compatible
>=5.1

Installation

Add the package in your composer.json by executing the command.

composer require waska14/laravel-uuid

Next, add the service provider in your config/app.php

'providers' => [
    ....
    Waska\LaravelUuid\UuidServiceProvider::class,
    ....
],

Migrations

// 2.1 If you want to use uuid column as primary column:
Schema::create('students', function(Blueprint $table)
{
    $table->uuid('uuid')->primary();
    $table->string('name');
    $table->string('last_name');
    ...
});

// 2.2 If you want to use uuid column as non-primary column:
Schema::create('students', function(Blueprint $table)
{
    $table->increments('id');
    $table->uuid('uuid')->unique();
    $table->string('name');
    $table->string('last_name');
    ...
});

// 2.3 If you want to use multiple uuid columns (maybe on of them is primary):
Schema::create('students', function(Blueprint $table)
{
    $table->uuid('uuid')->primary();
    $table->uuid('uuid_column_2')->unique();
    $table->uuid('uuid_column_3')->unique();
    $table->string('name');
    $table->string('last_name');
    ...
});

Note: If uuid column is not primary, you should make it unique, or manually index the column.

Models

  1. You must use trait in model
class Student extends Model
{
    use \Waska\Traits\Uuid;
}

2.1 If you are using uuid column as primary key and column_name is default id, you need nothing more.

2.2 If you are using uuid column as primary key and column_name is not default id, you must define protected $primaryKey and protected $uuid_column:

class Student extends Model
{
    use \Waska\Traits\Uuid;

    protected $primaryKey = "uuid_primary_column_name";
    protected $uuid_column = "uuid_primary_column_name";

    protected $fillable = [
        'name',
        'last_name',
    ];
}

2.3 If you are using non-primary uuid column and column name equals to default_column_name (uuid from config), you need only to append column name in protected $fillable:

class Student extends Model
{
    use \Waska\Traits\Uuid;

    protected $fillable = [
        'name',
        'last_name',
        'uuid', // config/waska.uuid.php -> default_column_name
    ];
}

2.4 If you are using non-primary uuid column and column name doesn't equal to default_column_name (uuid from config), you need define protected $uuid_column and append column name in protected $fillable:

class Student extends Model
{
    use \Waska\Traits\Uuid;

    protected $uuid_column = "uuid_column_name";
    
    protected $fillable = [
        'name',
        'last_name',
        'uuid_column_name',
    ];
}

2.5 If you are using multiple uuid columns (if one of them is primary, you must do step 2.1 at first), you need define protected $uuid_column as an array and append column names (only non-primary) in protected $fillable

class Student extends Model
{
    use \Waska\Traits\Uuid;

    protected $primaryKey = "uuid_primary_column_name"; // If one of them is primary
    protected $uuid_column = ["uuid_column_name1", "uuid_column_name2", "uuid_column_name3"];
    
    protected $fillable = [
        'name',
        'last_name',
        'uuid_column_name1',
        'uuid_column_name2',
        'uuid_column_name3',
    ];
}

2.6 Now when you create new Student object, uuid(s) will be generated automatically

Student::create([
    'name'      => "Bill',
    'last_name' => "Gates',
]);

// or
$student = new Student([
    'name'      => "Bill',
    'last_name' => "Gates',
]);
$student->save();

// or
$student = new Student();
$student->name = "Bill";
$student->last_name = "Gates";
$student->save();

Configuration

If you want to change default configuration, you must publish default configuration file to your project.

You can publish config by running this in console:

php artisan vendor:publish --tag=waska-uuid-config

Note: If you're going to use v3 or v5 uuid, it's recommended to change v3_default_namespace and v5_default_namespace with valid uuid strings .

Generate them with this command in tinker (Start tinker: php artisan tinker)

Waska\Uuid::get(4); // This command will generate valid uuid (v4, pseudo-random) string

Other usage

Generate uuid (Universal Unique Identifier)

/**
 * This generates name-based Uuid
 * @param int $version.
 * @param string $name. String which the uuid is generating for.
 * @param string $namespace. Valid uuid string. Default value is defined in config/waska.uuid.php
 * @return String
 */
Waska\Uuid::get(3, "some_random_string", "valid_uuid");

// This generates pseudo-random Uuid
Waska\Uuid::get(); // Default version is 4, so it means: Waska\Uuid::get(4);

/**
 * This generates name-based Uuid
 * @param int $version.
 * @param string $name. String which the uuid is generating for.
 * @param string $namespace. Valid uuid string. Default value is defined in config/waska.uuid.php
 * @return String
 */
Waska\Uuid::get(5, "some_random_string", "valid_uuid");

waska14/laravel-uuid 适用场景与选型建议

waska14/laravel-uuid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.13k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 11 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「uuid」 「laravel」 「generate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 waska14/laravel-uuid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 waska14/laravel-uuid 我们能提供哪些服务?
定制开发 / 二次开发

基于 waska14/laravel-uuid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 16.13k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-11-01