PHPackages                             madnh/laravel-model-labels - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. madnh/laravel-model-labels

ActiveLibrary[Localization &amp; i18n](/categories/localization)

madnh/laravel-model-labels
==========================

Support get label of model's fields, from model::$labels property or Laravel's localization features

0.4.1(9y ago)73.1k3[1 issues](https://github.com/madnh/laravel-model-labels/issues)MITPHPPHP &gt;=5.4.0

Since Dec 30Pushed 9y ago1 watchersCompare

[ Source](https://github.com/madnh/laravel-model-labels)[ Packagist](https://packagist.org/packages/madnh/laravel-model-labels)[ RSS](/packages/madnh-laravel-model-labels/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (5)Dependencies (1)Versions (6)Used By (0)

LaravelModelLabels
==================

[](#laravelmodellabels)

Support get label of model's fields, from model::$labels property or Laravel's localization features

Install
-------

[](#install)

1. Add this package to `composer.json`

```
composer require madnh/laravel-model-labels
```

2. In model classes, use `MaDnh\LaravelModelLabels\LabelsTrait`

```
use MaDnh\LaravelModelLabels\LabelsTrait;

class Country extends Model
{
    use SoftDeletes, LabelFieldTrait;

   //Model contents...
```

Properties
----------

[](#properties)

1. **`static::$label_path`**

Prefix of locale path, default is `model_`. Example:

```
public static $label_path = 'flag'; //Default is model_flag
```

2. **`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'
];
```

3. **`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 =&gt; Id .

Special useful for acronym words like ID, VIP, CMND,..

Example:

```
public static $labels_trans_map = ['id' => 'ID']; //Auto convert label is Id
```

4. **`static::$label_cached`**

Cached labels. Priority is highest.

Usage
-----

[](#usage)

### Label priority

[](#label-priority)

Model cache &gt; Laravel i18n &gt; Model `static::$labels`

### Define 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

1. 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.')';
    }];

    //...
```

2. Define labels in locale files

Add file to site's locale folder, named `model_.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.

```
