一些用于laravel的猴子翻译服务和json字段。请不要安装它。我是认真的

1.5.7 2018-06-09 12:59 UTC

README

StyleCI

laravel ^5.* 和 php ^7.0 的一些有用工具,内部包含猴子代码 insine

可以在MIT协议下自由使用...

在2017年3月14日之后的0.0.3版本后进行包重构,使用此版本以保持兼容性

可用特性

Sluggable

简单地将源字段转换为slug字段,使用 Illuminate\Support\Str::slug 函数。

use Amari\Traits\Sluggable;

class Model extends \Illuminate\Database\Eloquent\Model
{
    use Sluggable;
    
    // Optional, default 'title'
    protected static $slugSource = 'name';
    
    // Optional, default 'slug'
    protected static $slugField = 'route';
    
    public static function boot()
    {
        parent::boot();

        static::saving(function (Model $item) {
            $item->generateSlug(); //Check dirty slug before save
        });
    }
    
    ...
}

如果未提供路由,此代码将从名称生成路由属性,如果数据库中已存在具有当前slug的实体,则slug将附加"-$index"从1到无限,除非脚本找到空闲...

Jsonable

草地上的另一个猴子。现在您可以像访问模型的一般属性一样访问json字段。(仅限于1-2级)

工作原理

use Amari\Traits\Jsonable;

class Model extends \Illuminate\Database\Eloquent\Model
{
    use Jsonable;
    
    protected $fillable = ['title', 'header_content', 'block2', 'block3', 'images', 'meta-title', 'keywords', 'description']
    
    // Required static field with map: real model attribute to json attributes, that will be save in real
    protected static $json = [
        'body' => [
            'header_content',
            'block2',
            'block3',
            'images'
        ],
        'meta' => [
            'meta-title' //it's must be unique (not title, if real title exists) to other or it have more priority agains real fields
            'keywords',
            'description',
        ],
        ...
    ];
    
    ...
}

现在您可以像通常一样访问第二级属性

$model = new Model([
    'title' => 'Awesome monkey!', 
    'header_content' => 'Wellcome again!', 
    'block3' => 'brilliant brilliant!', 
    'images' => [
        'image1' => '/path/to/image/1.png',
        'image2' => '/path/to/image/2.png',
        'image3' => '/path/to/image/3.png',
    ]
]);

$model->block2 = 'Some test';

dd(model->header_content); //Wellcome again!
dd(model->images); //Array [...]
dd(model->block2); //Some test

...