madnh/laravel-model-labels
最新稳定版本:0.4.1
Composer 安装命令:
composer require madnh/laravel-model-labels
包简介
Support get label of model's fields, from model::$labels property or Laravel's localization features
README 文档
README
Support get label of model's fields, from model::$labels property or Laravel's localization features
Install
- Add this package to
composer.json
composer require madnh/laravel-model-labels
- In model classes, use
MaDnh\LaravelModelLabels\LabelsTrait
use MaDnh\LaravelModelLabels\LabelsTrait; class Country extends Model { use SoftDeletes, LabelFieldTrait; //Model contents...
Properties
static::$label_path
Prefix of locale path, default is model_<model_name_in_snake_case>. Example:
public static $label_path = 'flag'; //Default is model_flag
static::$labels
Model labels. Array of properties name (as keys) and labels (as value)
Example:
public static $labels = [ 'id' => 'ID', 'full_name' => 'Họ và tên' ];
static::$labels_trans_map
Label trans map, use when can't find label of a property in label cached, locale, static $labels.
If the property is not defined in this array, will use the auto convert function - which try to get the label in title case of lower cased property name: id => Id .
Special useful for acronym words like ID, VIP, CMND,..
Example:
public static $labels_trans_map = ['id' => 'ID']; //Auto convert label is Id
static::$label_cached
Cached labels. Priority is highest.
Usage
Label priority
Model cache > Laravel i18n > Model static::$labels
Define labels
Label can store in a static property of model, or in a locale file. Labels in locale files will override model's static property
- Define labels in model class
Define a static property named $labels, it is an array of labels, with key is fields name, value is label.
Label maybe a string or callable value. If it's a callable value, then result of it will used as label, that callable has 1 argument, it is the field need to get label.
class Country extends Model { use SoftDeletes, LabelFieldTrait; //... public static $labels = [ 'name' => 'Name of country', 'flag' => function($field){ return 'Flag ('.$field.')'; }]; //...
- Define labels in locale files
Add file to site's locale folder, named model_<model_name_in_snake_case>.php.
This locale file return an array of string. Labels of model's fields store at a key named is field, it is an array of field name and label.
<?php return [ 'field' => [ 'name' => 'Tên quốc gia' ] ];
Get labels
Use following methods to get labels
| Method | Example | Description |
|---|---|---|
| modelLabel() | Country::modelLabel() |
Get label of model class |
| labels() | Country::labels() |
Get all of lables of fields |
| labels() | Country::labels('name', 'flag', 'code', ['title']) |
Get label of fields |
| label() | Country::label('name') |
Get label of a field |
Examples
namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; use App\Models\Country; class CreateCountryRequest extends FormRequest { public function rules() { return [ 'name' => 'required' ]; } public function attributes() { return Country::labels('name'); }
madnh/laravel-model-labels 适用场景与选型建议
madnh/laravel-model-labels 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.13k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2016 年 12 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「model」 「laravel」 「localization」 「fields」 「label」 「laravel 5」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 madnh/laravel-model-labels 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 madnh/laravel-model-labels 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 madnh/laravel-model-labels 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Package for convenient work with Laravel's localization features
Field for number with restricted count of digits and decimal places
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Deepl Automated translations for Laravel lang files
Interfaces for web resource models and services to retrieve and create them
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 3.13k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2016-12-30