echosters/translatable

Translatable 是一个 Laravel 包,帮助您处理多语言数据表

v1.0.5 2022-06-12 00:31 UTC

This package is auto-updated.

Last update: 2024-09-30 01:54:21 UTC


README

Translatable 是一个用于处理多语言表的 Laravel 包。

安装

使用 composer 安装 translatable。

composer require echosters/translatable

数据库

准备您的模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Echosters\Translatable\Translatable;

class Blog extends Model
{
    use Translatable;

    public $translatedColumns = ['title'];
}

用法

//local = en
$blog = Blog::find(1);
$blog->title; // Hello

//local = ar
$blog = Blog::find(1);
$blog->title; // مرحبا

查询

您可以使用 whereLocale 根据当前区域查询一个可翻译列

//local = en
$blog = Blog::whereLocal('title', 'LIKE', '%hell%')->first();
$blog->title; // Hello

//local = ar
$blog = Blog::whereLocal('title', 'LIKE', '%مرح%')->first();
$blog->title; // مرحبا

注意

只需知道,title 属性在您的表中不存在,特性将在模型启动时添加它,换句话说,生成的 SQL 语句如下。

当区域为 en 时,语句select title_en as title

当区域为 ar 时,语句select title_ar as title

贡献

欢迎拉取请求。对于重大更改,请先创建一个问题来讨论您想进行什么更改。

许可证

MIT