weremagine / laravel-traits
为 Laravel 使用的有观点的 Traits 集合。
Requires
- jenssegers/agent: ^2.6
README
REMAGINE 编写的 Laravel 的有观点 traits 集合。
composer require weremagine/laravel-traits
目录
HasCreator
HasCreator 在实现它的模型创建记录时,自动将当前授权用户的 id 添加到 user_id 列。
用法
use Remagine\Traits\HasCreator; class Article extends Model { use HasCreator;
HasImages
HasImages 在检索实现它的模型时,自动附加图像。
imagesArray 函数 *必需 包含多维数组的字符串
attribute: 要附加到模型的属性名称。field: 包含图像路径的模型字段。
imagePrefix 函数 (可选) 预缀所有附加属性。如果省略,则返回指定的字段,如存储那样。
用法
use Remagine\Traits\HasImages; class Article extends Model { use HasImages; /** * Images to append to the model via HasImages. * * @return array */ public function imagesArray() { return [ [ 'attribute' => 'image_url', 'field' => 'image', ], ]; } /** * The string to prefix image attributes with. * * @returns string */ public function imagePrefix() { return env('AWS_CDN').'/'; }
HasOne
HasOne 自动添加一个不同的相关模型关系,其中
$has_one_attribute 属性 (可选) 设置关系时使用的名称,默认为 model。
$has_one_type 属性 (可选) 包含相关模型类路径的字段名称,例如值 App\Models\Menu - 默认为 model_type。
$has_one_id 属性 (可选) 包含相关模型 id 的字段名称 - 默认为 model_id。
用法
use Remagine\Traits\HasOne; class QrCode extends Model { use HasOne; /** * The name to give the relationship via the HasOne trait. * * @var string */ protected $has_one_attribute = 'model'; /** * The name of the field to lookup the HasOne model. * * @var string */ protected $has_one_type = 'model_type'; /** * The name of the field containing the HasOne model's id. * * @var string */ protected $has_one_id = 'model_id';
HasSlug
HasSlug 在保存(创建或更新)实现它的任何模型时自动设置别名。
$sluggify 属性 (可选) 创建别名时使用的字段名称 - 默认为 name。
用法
use Remagine\Traits\HasSlug; class Article extends Model { use HasSlug; /** * The attribute to sluggify when saving. * * @var string */ protected $sluggify = 'title';
HasUniqueKey
HasUniqueKey 在为实现它的模型创建记录时自动设置唯一键。
$unique_key 属性 (可选) 创建唯一键时使用的字段名称 - 默认为 key。
$unique_key_length 属性 (可选) 希望唯一键的长度 - 默认为 10。
$uppercase_key 属性 (可选) 一个布尔值,表示是否应将键大写 - 默认为 false。
$key_prefix 属性 (可选) 唯一键的前缀。 - 默认为 null。
$key_suffix 属性 (可选) 唯一键的后缀。 - 默认为 null。
用法
use Remagine\Traits\HasUniqueKey; class Article extends Model { use HasUniqueKey; /** * The name of the unique key for the model. */ protected $unique_key = 'key'; /** * The length unique keys generated should be. */ protected $unique_key_length = 10; /** * Indicates if the key should be uppercased. */ protected $uppercase_key = false; /** * The prefix for the unique key. */ protected $key_prefix = null; /** * The suffix for the unique key. */ protected $key_suffix = null;
HasUuid
HasUuid 在为实现它的模型创建记录时自动设置 uuid。
$uuid_field 属性 (可选) 创建 uuid 时使用的字段名称 - 默认为 uuid。
用法
use Remagine\Traits\HasUuid; class Article extends Model { use HasUuid; /** * The name of the field to store uuids. */ protected $uuid_field = 'uuid';
方法
byUuid 使用 UUID 检索记录。
Article::byUuid($uuid);
UserAgent
UserAgent 在存储模型的新记录时,自动将请求中指定的 user-agent 标头添加到 agent 字段。
$parse_agent_fields 属性 (可选) 一个布尔值,表示是否解析代理字符串并在创建时存储 - 默认为 false。
以下字段(可为空字符串)将被存储
- platform
- platform_version
- browser
- browser_version
- device
- device_name
用法
use Remagine\Traits\UserAgent; class Article extends Model { use UserAgent; /** * Indicates if the agent should be parsed when saving. * * @var array */ protected $parse_agent_fields = false;