joelshepherd / create-with
轻松添加常见的唯一身份字段到Laravel模型。
0.4.0
2017-09-13 06:34 UTC
Requires
- php: >=7.0
- illuminate/database: ^5.0
- illuminate/http: ^5.0
- illuminate/support: ^5.0
- ramsey/uuid: ^3.6
Requires (Dev)
- phpunit/phpunit: ^6.2
This package is not auto-updated.
Last update: 2024-09-15 04:00:18 UTC
README
一个简单的包,提供特性以在创建Laravel模型时添加常见的身份字段。
此包设计为仅使用特性即可开箱即用。不需要其他配置。
安装
依赖项
- PHP 7
- Laravel 5.*
Composer
composer require joelshepherd/create-with
使用方法
简单地将特性添加到提供所需字段的模型中。如果字段不为空且在数据库中是唯一的,它将保持不变。
使用UUID创建
为模型添加唯一的UUID。
默认选项
getUuidField()
返回uuid
<?php use JoelShepherd\CreateWith; class Example extends Model { use CreateWith\Uuid; }
<?php $example = Example::create(); $example->uuid; // 123e4567-e89b-12d3-a456-426655440000
使用slug创建
为模型添加唯一的slug。这可以基于文本字符串(如模型上的标题字段)并附加随机slug以保持唯一性(如果需要)。
默认选项
getSlugField()
返回slug
getSlugBaseText()
返回null
getSlugRandomLength()
返回7
<?php use JoelShepherd\CreateWith; class Example extends Model { use CreateWith\Slug; // Optionally set the base string to build the slug from protected function getSlugBaseText() { return $this->title; } }
<?php // Creates a unique slug from the base text $example = Example::create([ 'title' => 'This is a title' ]); $example->slug; // this-is-a-title // Uniqueness is retained even with the same base text $example2 = Example::create([ 'title' => 'This is a title' ]); $example2->slug; // this-is-a-title-7iw90lj
使用IP地址创建
将请求者的IP地址添加到模型中。
默认选项
getIpAddressField()
返回ip_address
<?php use JoelShepherd\CreateWith\WithIpAddress; class Example extends Model { use CreateWith\IpAddress; }
<?php $example = Example::create(); $example->ip_address; // 127.0.0.1
贡献
提交有关错误、功能和功能请求的问题和拉取请求是欢迎的。