Astrotomic / laravel-translatable

A Laravel package for multilingual models
https://docs.astrotomic.info/laravel-translatable/
MIT License
1.23k stars 156 forks source link

Can't update model with new translation #232

Closed sheinfeld closed 3 years ago

sheinfeld commented 3 years ago

Hi guys,

So I start using this package, amazing work btw, but for some reason, I am unable to update a translation with the following code:

<?php
/*
 * Copyright (c) 2021 Ceuton.
 */

namespace App\Console\Commands;

use App\Models\Article;
use BabyMarkt\DeepL\DeepL;
use Carbon\Carbon;
use DB;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
use Throwable;

class TranslateArticles extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'translate:articles';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    public $deepl;

    public array $locales = [
        'en',
        /*'es',
        'fr',
        'de'*/
    ];

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->deepl = new DeepL(config('constants.api_keys.deepl'));

        try {
            $articles = Article::whereHas('translations', function (Builder $q) {
                $q->whereIn('locale', ['pt', 'en']);
            })/*where('created_at', '>=', Carbon::now()->subMinutes(10))*/
                ->orderByDesc('created_at')
                ->take(10)
                ->get();

            foreach ($articles as $article) {
                foreach ($this->locales as $locale)
                    $this->article($article, $locale, $locale == 'en' ? 'pt' : 'en');
            }

        } catch (Throwable $exception) {
            echo sprintf("[%d] %s %s\n", $exception->getLine(), $exception->getMessage(), $exception->getFile());
        }

        return 0;
    }

    /**
     * @throws Throwable
     */
    public function article($article, $locale_trg, $locale_src)
    {
        DB::beginTransaction();

        try {
            if($article->translations()->where('locale', $locale_trg)->exists()) {
                echo sprintf("Article [%s => %s][Skipping]: %s\n", $locale_src, $locale_trg, $article->getTitle());
                return;
            } else
                echo sprintf("Article [%s => %s]: %s\n", $locale_src, $locale_trg, $article->getTitle());

            $data = [
                'slug' => $this->deepl->translate($article->slug, $locale_src, $locale_trg)[0]['text'] . PHP_EOL,
                'title' => $this->deepl->translate($article->title, $locale_src, $locale_trg)[0]['text'] . PHP_EOL,
                'body' => $this->deepl->translate($article->body, $locale_src, $locale_trg)[0]['text'] . PHP_EOL,
            ];

            foreach ($data as $cell) if(!$cell) return;

            $article->fill([
                $locale_trg => $data
            ]);

            DB::commit();

        } catch (Throwable $exception) {
            DB::rollBack();
            echo sprintf("[%d] %s %s\n", $exception->getLine(), $exception->getMessage(), $exception->getFile());
        }
    }
}

Any of you can help me out? I tried ->fill() and ->update() but I still can't manage to save it to the database...