定制 vershub/laraveltranslations 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vershub/laraveltranslations

最新稳定版本:v1.0.1

Composer 安装命令:

composer require vershub/laraveltranslations

包简介

laravel package for translatable models

README 文档

README

Laravel Translations

This lightweight Laravel package enables you to eager load models along with their translations in the specified language, efficiently preventing the N+1 query problem. Additionally, it allows you to retrieve a single translation (instead of an array) without unnecessarily loading extra models into memory..

Latest Stable Version Total Downloads License

🚀 Installation

Install the package via Composer:

composer require vershub/laraveltranslations

🛠️ Usage

1. Create a Translatable Model

Extend the TranslatableModel class and implement the required abstract methods:

namespace App\Models;  

use Vershub\LaravelTranslations\TranslatableModel;  

class CarBrand extends TranslatableModel  
{  
    protected $fillable = ['name'];  

    protected function getTranslationModel(): string  
    {  
        return CarBrandTranslation::class;  
    }  

    protected function getForeignKeyForTranslation(): string  
    {  
        return 'car_brand_id';  
    }  
}

2. Create a Translation Model

The translation model should be a standard Eloquent model:

namespace App\Models;  

use Illuminate\Database\Eloquent\Model;  

class CarBrandTranslation extends Model  
{  
    protected $fillable = ['locale_code', 'name', 'description'];  
}

Example of translation table migration:

use Illuminate\Database\Migrations\Migration;  
use Illuminate\Database\Schema\Blueprint;  
use Illuminate\Support\Facades\Schema;  

class CreateCarBrandTranslationsTable extends Migration  
{  
    public function up()  
    {  
        Schema::create('car_brand_translations', function (Blueprint $table) {  
            $table->id();  
            $table->foreignId('car_brand_id')->constrained()->cascadeOnDelete();  
            $table->string('locale_code');  
            $table->string('name');  
            $table->text('description')->nullable();  
            $table->timestamps();  
        });  
    }  

    public function down()  
    {  
        Schema::dropIfExists('car_brand_translations');  
    }  
}  

3. Use the withTranslation Scope

Retrieve translations for the current locale

use App\Models\CarBrand;  

$carBrands = CarBrand::withTranslation()->get();  

foreach ($carBrands as $carBrand) {  
    echo $carBrand->translation->name;  
}

Retrieve translations for a specific locale

use App\Models\CarBrand;  

$carBrands = CarBrand::withTranslation('fr')->get();  

foreach ($carBrands as $carBrand) {  
    echo $carBrand->translation->name;  
}

Select specific columns from translations table

use App\Models\CarBrand;  

$carBrands = CarBrand::withTranslation('fr:id,locale_code,name,description')->get();  

⚙️ Customization

Override the Locale Code Column

By default, the package uses locale_code. You can change this by publishing the config file and change the value of locale_code_column key:

return [
    'locale_code_column' => 'locale_code'
];
    

统计信息

  • 总下载量: 64
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固