jamesmills/eloquent-uuid
最新稳定版本:1.6.1
Composer 安装命令:
composer require jamesmills/eloquent-uuid
包简介
A Laravel Eloquent Model trait for adding and using a uuid with models
README 文档
README
A Laravel Eloquent Model trait for adding and using a uuid with models.
The trait listens to the creating event. It generates a new UUID and saves it in the uuid column on the model.
Featured in Laravel News
Installation
composer require jamesmills/eloquent-uuid
Use
In order to use this in your models, just put use HasUuidTrait;
<?php namespace App; use JamesMills\Uuid\HasUuidTrait; class User extends Eloquent { use HasUuidTrait; }
Schema requirements
In order to use this trait, your schema must be something like:
<?php // ... Schema::create('users', function (Blueprint $table) { $table->primary('id'); $table->uuid('uuid')->unique(); // this will create a CHAR(36) field $table->string('username', 32); $table->string('password', 50); // ... });
Querying your models
You may use the findByUuidOrFail method to try and fetch a model directly:
<?php Route::get('/user/{uuid}', function($uuid) { try { return App\User::findByUuidOrFail($uuid); } catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) { abort(404); } });
Since uuid gets registered as Route Key using implicit binding[https://laravel.com/docs/5.8/routing#implicit-binding], your resource controllers will use uuid instead of default id column.
<?php php artisan make:controller UserController --resource
/users/{user} route uses uuid i.e. /users/bff37872-1450-47c7-b9f7-9a6d917796cf
You may also use the withUuid and withUuids local query scopes with the query builder.
<?php Route::get('/user/{uuid}', function($uuid) { $user = App\User::withUuid($uuid)->first(); if (! $user) { // Do something else... } });
<?php Route::delete('/users', function(Request $request) { // Receive an array of UUIDs $uuids = $request->input('uuids'); // Try to get the Users $users = App\User::withUuids($uuids)->all(); // Handle the delete and return $users->delete(); });
Licence
This package is 100% free and open-source, under the MIT license. Use it however you want.
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
jamesmills/eloquent-uuid 适用场景与选型建议
jamesmills/eloquent-uuid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 43.73k 次下载、GitHub Stars 达 139, 最近一次更新时间为 2016 年 03 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「uuid」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jamesmills/eloquent-uuid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jamesmills/eloquent-uuid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jamesmills/eloquent-uuid 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Help to manage meta data on Laravel Eloquent model
A package to cast json fields, each sub-keys is castable
DrUUID RFC 4122 library for PHP
Trait to generate uuid when creating models
统计信息
- 总下载量: 43.73k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 139
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-03-14