2017 © Pedro PelĂĄez
 

library laravel-model-labels

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

image

madnh/laravel-model-labels

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

  • Wednesday, March 1, 2017
  • by madnh
  • Repository
  • 2 Watchers
  • 2 Stars
  • 256 Installations
  • PHP
  • 0 Dependents
  • 1 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 11 % Grown

The README.md

LaravelModelLabels

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

Install

  1. Add this package to composer.json
composer require madnh/laravel-model-labels
  1. In model classes, use MaDnh\LaravelModelLabels\LabelsTrait
use MaDnh\LaravelModelLabels\LabelsTrait;

class Country extends Model
{
    use SoftDeletes, LabelFieldTrait;

   //Model contents...

Properties

  1. static::$label_path

Prefix of locale path, default is model_<model_name_in_snake_case>. Example:, (*2)

public static $label_path = 'flag'; //Default is model_flag
  1. static::$labels

Model labels. Array of properties name (as keys) and labels (as value), (*3)

Example:, (*4)

public static $labels = [
    'id' => 'ID',
    'full_name' => 'Họ vĂ  tĂȘn'
];
  1. static::$labels_trans_map

Label trans map, use when can't find label of a property in label cached, locale, static $labels., (*5)

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 ., (*6)

Special useful for acronym words like ID, VIP, CMND,.., (*7)

Example:, (*8)

public static $labels_trans_map = ['id' => 'ID']; //Auto convert label is Id 
  1. static::$label_cached

Cached labels. Priority is highest., (*9)

Usage

Label priority

Model cache > Laravel i18n > Model static::$labels, (*10)

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, (*11)

  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., (*12)

class Country extends Model
{
    use SoftDeletes, LabelFieldTrait;

    //...

    public static $labels = [
        'name' => 'Name of country',
        'flag' => function($field){
            return 'Flag ('.$field.')';
    }];

    //...
  1. 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., (*13)

<?php
return [
    'field' => [
        'name' => 'TĂȘn quốc gia'
    ]
];

Get labels

Use following methods to get labels, (*14)

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

The Versions

01/03 2017

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

laravel model localization laravel 5 fields label

01/03 2017

0.4.1

0.4.1.0

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

  Sources   Download

MIT

The Requires

 

laravel model localization laravel 5 fields label

01/03 2017

0.4

0.4.0.0

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

  Sources   Download

MIT

The Requires

 

laravel model localization laravel 5 fields label

18/01 2017

0.3

0.3.0.0

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

  Sources   Download

MIT

The Requires

 

laravel model localization laravel 5 fields label

05/01 2017

0.2

0.2.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

laravel model localization laravel 5 fields label

30/12 2016

0.1

0.1.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

laravel model localization laravel 5 fields label