Translate eloquent entity models

Overview

SensioLabsInsight

Translate entity

About package

This package is intended for adding multi language support to your models. If your application already be working and you need to add one or more aditional languages to your content in database it's be easy.

Installation

Require this package in your composer.json

$ composer reqiure vortgo/laravel-translate

Add the service provider to you config file config/app.php

Vortgo\Translate\ModelTranslateServiceProvider::class

Publish vendor

    $ php artisan vendor:publish

Run migration to create table for your translatable content

$ php artisan migrate

Add trait to your model which need to translate and setup your default language for your model

    class Category extends Model
    {
        use Translate;
        protected $defaultLocale = 'en';
    }

Usage

You can create entity with translate as usually:

        Category::create([
            'name' => 'name',
            'ru' => [
                'name' => 'название'
            ],
            'fr' => [
                'name' => 'fr name'
            ]
        ]);

For access the translate value you can use next variant:

Determine when calling

    $category->translate('fr')->name

Use your app locale

    App()->setLocale('fr')
    $category->name

Get all translations attributes for current model

$category->getTranslations('fr')

Bonuses

You can use relations for eager loader your model

    App()->setLocale('fr');
    $item = Item::with('category', 'category.rTranslate')->first();
    $item->category->name;

To array with relation

    App()->setLocale('fr');
    $item = Item::with('category', 'category.rTranslate')->first();
    $item->toArray();
    Result = [
        'id' =>1,
        'item_name' => 'name',
        'category' => [
            'name' => 'fr name'
        ]
    ];

If you want to override function toArray() use translateToArray() in your model

    public function toArray()
    {
        $array = $this->translateToArray(); //parent::toArray()

        // Your code here

        return $array;
    }

You might also like...
Use auto generated UUID slugs to identify and retrieve your Eloquent models.

Laravel Eloquent UUID slug Summary About Features Requirements Installation Examples Compatibility table Alternatives Tests About By default, when get

Record created by, updated by and deleted by on Eloquent models automatically.

quarks/laravel-auditors Record created by, updated by and deleted by (if SoftDeletes added) on Eloquent models automatically. Installation composer re

Observe (and react to) attribute changes made on Eloquent models.
Observe (and react to) attribute changes made on Eloquent models.

Laravel Attribute Observer Requirements PHP: 7.4+ Laravel: 7+ Installation You can install the package via composer: composer require alexstewartja/la

Tag support for Laravel Eloquent models - Taggable Trait

Laravel Taggable Trait This package is not meant to handle javascript or html in any way. This package handles database storage and read/writes only.

Preferences for Laravel Eloquent models

Preferences for Laravel Eloquent models Use this library to bind multiple key/value pair preferences to your application's Eloquent models. Preference

Laravel package to search through multiple Eloquent models.

Laravel package to search through multiple Eloquent models. Supports sorting, pagination, scoped queries, eager load relationships and searching through single or multiple columns.

Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.
Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.

Laravel Befriended Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked

Cascading deletes for Eloquent models that implement soft deletes

Cascading soft deletes for the Laravel PHP Framework Introduction In scenarios when you delete a parent record - say for example a blog post - you may

A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.

Introduction A simple drop-in solution for providing UUID support for the IDs of your Eloquent models. Both v1 and v4 IDs are supported out of the box

Releases(1.0.0)
Owner
null
Laravel Larex lets you translate your whole Laravel application from a single CSV file.

Laravel Larex Translate Laravel Apps from a CSV File Laravel Larex lets you translate your whole Laravel application from a single CSV file. You can i

Luca Patera 68 Dec 12, 2022
An Eloquent Way To Filter Laravel Models And Their Relationships

Eloquent Filter An Eloquent way to filter Eloquent Models and their relationships Introduction Lets say we want to return a list of users filtered by

Eric Tucker 1.5k Jan 7, 2023
Easy creation of slugs for your Eloquent models in Laravel

Eloquent-Sluggable Easy creation of slugs for your Eloquent models in Laravel. NOTE: These instructions are for the latest version of Laravel. If you

Colin Viebrock 3.6k Dec 30, 2022
Sortable behaviour for Eloquent models

Sortable behaviour for Eloquent models This package provides a trait that adds sortable behaviour to an Eloquent model. The value of the order column

Spatie 1.2k Dec 22, 2022
This package gives Eloquent models the ability to manage their friendships.

Laravel 5 Friendships This package gives Eloquent models the ability to manage their friendships. You can easily design a Facebook like Friend System.

Alex Kyriakidis 690 Nov 27, 2022
Automatically validating Eloquent models for Laravel

Validating, a validation trait for Laravel Validating is a trait for Laravel Eloquent models which ensures that models meet their validation criteria

Dwight Watson 955 Dec 25, 2022
Laravel Ban simplify blocking and banning Eloquent models.

Laravel Ban Introduction Laravel Ban simplify management of Eloquent model's ban. Make any model bannable in a minutes! Use case is not limited to Use

cybercog 879 Dec 30, 2022
cybercog 996 Dec 28, 2022
Create presenters for Eloquent Models

Laravel Presentable This package allows the information to be presented in a different way by means of methods that can be defined in the model's pres

The Hive Team 67 Dec 7, 2022
A small package for adding UUIDs to Eloquent models.

A small package for adding UUIDs to Eloquent models. Installation You can install the package via composer: composer require ryangjchandler/laravel-uu

Ryan Chandler 40 Jun 5, 2022