xt/laravel-external-id
最新稳定版本:v1.0.3
Composer 安装命令:
composer require xt/laravel-external-id
包简介
Generate external id when saving Eloquent models
README 文档
README
This package provides a trait that will generate a public usage id when saving any Eloquent model.
$model = new EloquentModel(); $model->save(); echo $model->external_id; // ouputs "activerecord-is-awesome"
Installation
You can install the package via composer:
composer require xt/laravel-external-id
Usage
Your Eloquent models should use the XT\ExternalId\HasExternalId trait and the XT\ExternalId\ExternalIdOptions class.
The trait contains an abstract method getExternalIdOptions() that you must implement yourself.
Your models' migrations should have a field to save the generated external id to.
Here's an example of how to implement the trait:
namespace App; use XT\ExternalId\HasExternalId; use XT\ExternalId\ExternalIdOptions; use Illuminate\Database\Eloquent\Model; class YourEloquentModel extends Model { use HasExternalId; /** * Get the options for generating the slug. */ public function getExternalIdOptions() : ExternalIdOptions { return ExternalIdOptions::create() ->saveExternalIdTo('external_id'); } }
With its migration:
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateYourEloquentModelTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('your_eloquent_models', function (Blueprint $table) { $table->increments('id'); $table->string('external_id'); // Field name same as your `saveExternalIdTo` $table->string('name'); $table->timestamps(); }); } }
You can also specify prefix, length, numeric id only, time based id or create your own custom id
namespace App; use XT\ExternalId\HasExternalId; use XT\ExternalId\ExternalIdOptions; use Illuminate\Database\Eloquent\Model; class YourEloquentModel extends Model { use HasExternalId; /** * Get the options for generating the slug. */ public function getExternalIdOptions() : ExternalIdOptions { // generate alpha numeric id of particular length return ExternalIdOptions::create() ->saveExternalIdTo('external_id') ->setLength(16); // Generate id with prefix return ExternalIdOptions::create() ->saveExternalIdTo('external_id') ->setPrefix('order_'); // Output: order_<generated_id> // Generate numeric id of particular length return ExternalIdOptions::create() ->saveExternalIdTo('external_id') ->setLength(16) ->setIsNumberOnly(true); //Optional // Generate incremental id return ExternalIdOptions::create() ->saveExternalIdTo('external_id') ->incremental(1); // increment start from 1. Default value is: 1 // Generate unix timestamp based id return ExternalIdOptions::create() ->saveExternalIdTo('external_id') ->setIsTimeBase(true); // Output: 16628769732187896 // You can create your own id generation logic return ExternalIdOptions::create() ->saveExternalIdTo('external_id') ->customIdScope(function () { return getRandomString(6, false).getRandomNumber(6); }); } }
To get the record using external id, you can use findByExternalId method
YourEloquentModel::findByExternalId('<Your-ID>'); YourEloquentModel::findByExternalId('<Your-ID>', ['column1', 'column2']); OR YourEloquentModel::findByExternalIdOrFail('<Your-ID>'); YourEloquentModel::findByExternalIdOrFail('<Your-ID>', ['column1', 'column2']);
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 176
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-02