emden-norfolk/laravel-camelcase

此包已被废弃且不再维护。作者建议使用 kirkbushell/eloquence 包。
此包的最新版本(2.0.0)没有可用的许可证信息。

Eloquent 模型的 CamelCase 属性。

2.0.0 2019-09-28 04:27 UTC

This package is auto-updated.

Last update: 2023-02-18 04:14:35 UTC


README

此包为 Eloquent 模型添加了使用 CamelCase 规范的数据库字段支持。这些字段将作为 snake_case 属性公开。

注意事项:在 Laravel 架构中使用 CamelCase 不是一个好主意。该项目作为一个解决方案,允许开发人员与可能不遵循 Laravel 规范的旧数据库一起工作。

实验性

该项目是 实验性 的!请在 GitHub 问题跟踪器上提出任何问题。

安装

composer require emden-norfolk/laravel-camelcase

使用

示例

假设有一个名为 ExampleTable 的表,包含以下字段

  • ExampleID (整数,主键)
  • FooBarBaz (字符串)

一个示例实现可能如下所示

use EmdenNorfolk\Eloquent\Concerns\RichUpperCamelCase;

class ExampleTable extends Model
{
    use RichUpperCamelCase;
    
    protected $table = 'ExampleTable';
    protected $primaryKey = 'ExampleID';
}

上述模型可以如此访问

$example->example_id;
$example->foo_bar_baz = 'Lorem ipsum.';

这还将支持 mutators。

键转换

以下键转换受支持

  • EmdenNorfolk\Eloquent\Concerns\CamelCase --> exampleId
  • EmdenNorfolk\Eloquent\Concerns\UpperCamelCase --> ExampleId
  • EmdenNorfolk\Eloquent\Concerns\RichCamelCase --> exampleID
  • EmdenNorfolk\Eloquent\Concerns\RichUpperCamelCase --> ExampleID

也可以通过创建基于 trait TransformableKeys 的 trait 并实现 abstract function keyTransform 来创建自己的键转换。