Laravel 自动翻译 $request

Laravel auto translate $request

我的数据库中有两个 table。第一是工业,第二是industry_languages.

行业table

Schema::create('industries', function (Blueprint $table) {
        $table->increments('id');
        $table->string('industry_name', 100);
        $table->integer('language_id')->default(1);
        $table->timestamps();
    });

Industry_Languages' table

Schema::create('industry_languages', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('industry_id');
        $table->string('industry_name', 200);
        $table->integer('language_id');
        $table->timestamps();
    });

这是我的 IndustryController

public function store(Request $request)
{
    $industry = new Industry();
    $industry->industry_name = $request->industryName;
    $industry->save();

    return response()->json($industry);
}

此函数只会将数据存储到语言id = 1的行业table中,但我也想将其存储到语言id = 2的industry_languages'table中(因为我的数据库中有一种语言的 table,其中包含 2 行数据:'Indonesia'、'English')

示例:如果我在 industry_name 字段中输入 'Halo',它将在 industry_name 字段中作为 'Halo' 存储到我的行业 table 中,并且language_id 字段中的 '1',然后它还将存储到我的 industry_languages' table 中,作为 industry_name 字段中的 'Hello' 和 industry_name 中的 '2' language_id 字段

我在翻译行业 table 的请求时遇到问题。 我曾尝试使用 Laravel google-translate,但没有用

您尝试过使用 https://github.com/spatie/laravel-translatable 吗?

使用laravel-translatable你可以做类似

的事情
public function store(Request $request)
{
    $industry = new Industry(); 
    $industry
    ->setTranslation('industryName', 'en', 'Hello')
    ->setTranslation('industryName', 'es', $request->get('industryName'));

    $industry->save();

    return response()->json($industry);
}

对于其他语言,您可能需要使用翻译包

我试过this pakcage

但是,它需要 Google 翻译 API 密钥,我认为这不是免费的。所以,我找到了一个不需要生成 API KEY: statickidz/php-google-translate-free 的包