将不同语言的2个相同id的post表存储到数据库中

问题描述 投票:0回答:2

我正在建立一个多语言网站。在本例中,只有 2 种语言,即使用 Laravel 的印度尼西亚语和英语。 我有 posts 表,它将存储每个帖子的 id,post_translations 表用于存储帖子的本地、标题和描述。 我在将数据存储到数据库时遇到问题。我不知道如何在不包含 id 的情况下存储帖子,除了我添加了 2 个相同的印尼语和英语帖子。

这是结果(错误)

帖子表

id 
1
2

post_translations 表

id  post_id  locale   title
1     1      en       fisrt post
2     2      id       post yang pertama

预计结果

帖子表

id 
1

post_translations 表

id  post_id  locale   title
1     1      en       fisrt post
2     1      id       post yang pertama

后置控制器

  public function store(Request $request) {
    $this->validate($request, [
        'title' => 'required',
        'slug' => 'required',
        'content' => 'required'
    ]);

    $post = new Post;

    $post->title = $request->title;
    $post->slug = $request->slug;
    $post->content = $request->content;

    $post->save();
    return redirect()->route('post.index');
}
php mysql laravel database multilingual
2个回答
0
投票

好的,我们开始吧(请注意,这不是唯一的方法):

  1. 安装 spatie/laravel-translatable

    作曲家需要 spatie/laravel 可翻译

**注意:对于本机 spatie/laravel-translatable,请转到版本 2 **

  1. 创建一个具有以下结构的表:

    CREATE TABLE `articles` (
      `id` int(10) UNSIGNED NOT NULL,
      `title` text COLLATE utf8_unicode_ci,
      `slug` text COLLATE utf8_unicode_ci,
      `content` text COLLATE utf8_unicode_ci,
      `created_at` timestamp NULL DEFAULT NULL,
      `updated_at` timestamp NULL DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
    

注意:最好使用迁移。我刚刚导出了之前做的一个表来测试

  1. 将数据以json格式插入数据库,如下所示:

     INSERT INTO `pages` (`id`, `title`, `slug`, `content`, `created_at`, `updated_at`) VALUES
         (1, '{"ro":"Acas\\u0103","en":"Home"}', NULL, '{"ro":"<p><strong>Test title<\\/strong><\\/p>\\r\\n\\r\\n<p>Test content romanian<\\/p>\\r\\n","en":"<p><strong>test title english<\\/strong><\\/p>\\r\\n\\r\\n<p>test content english.<\\/p>\\r\\n"}', '2017-04-03 11:45:56', '2017-04-03 12:15:16');
    
  2. 现在创建刀片以编辑、更新、创建节目等。要获取语言,请在刀片中执行以下操作:

    {{ $data->title }}
    
    {!! nl2br($data->content) !!}
    
  3. 在控制器中: 添加这样的内容:

    /**
          * Generate the field by language
          *
          * @param   Model        $entry      the item selected from the database
          *
           * @return  array
          */
    
     public function getTranslatableFields($fields)
     {
         $inputs = [];
         $languages = $this->getLanguages();
    
         foreach ($languages as $language) {
             foreach ($fields as $field) {
                 $inputs[] = [
                     'name' => "{$field['name']}[{$language->abbr}]",
                     'label' => $field['label'] . " ($language->abbr)",
                     'lang' => $language->abbr,
                     'type' => array_key_exists('type', $field) ?     $field['type'] : 'text'
                 ];
             }
         }
    
         return $inputs;
     }
    

我在 LangTrait 中添加了这个函数。由于我也使用 Laravel 背包,所以我做了更多的事情。

为了编辑,我在特征中添加了这个方法:

/**
     * Show the form for editing the specified resource.
     *
     * @param   int             $id             the item's identifier
     *
     * @return  Response
     */
    public function edit($id)
    {    
        $data['entry'] = Model::find($id);
        $data['title'] = trans('lang_file.edit').' '.$this->entity_name; // name of the page 
        $data['fields'] = $this->getMultiLangFields($data['entry']);
        $data['id'] = $id;

        return view('crud::edit', $data);
    }


/**
     * Generate the field by language
     *
     * @param   Model        $entry      the item selected from the database
     *
     * @return  array
     */
    protected function getMultiLangFields($entry)
    {
        $fields['id'] = ['name' => 'id', 'type' => 'hidden', 'value' => $entry->id];

        foreach ($this->crud->update_fields as $key => $field) {
            $value = null;

            if (array_key_exists('lang', $field)) {
                $name = preg_replace('/(\[\w{2}\])$/i', '', $field['name']);
                $value = $entry->getTranslation($name, $field['lang']);
            }

            $fields[$key] = array_merge($field, ['value' => $value]);
        }

        return $fields;
    }

 

/**
     * Get the application active languages
     *
     * @return  \Backpack\LangFileManager\app\Models\Language
     */
    protected function getLanguages()
    {
        return Language::whereActive(1)->orderBy('default', 'desc')->get();
    }
  1. 在我的主控制器中我做了: use LangTrait;(包含以上所有内容)

    在构造中我添加了这个:

    $this->getTranslatableFields($fields)
    

    其中 $fields 是我需要的字段列表

    所有方法都应该适合您的 html 格式。正如我所说,我使用 Laravel 的背包,字段也相应地格式化。

  2. 最后为了让 getLanguage 文件正常工作,我在数据库中创建了一个新表和一个模型:

    型号:

    class Language extends Model
    {
        protected $table = 'languages';
    
        protected $fillable = ['name', 'flag', 'abbr', 'native', 'active', 'default'];
    
        public $timestamps = false;
    
        public static function getActiveLanguagesArray()
        {
            $active_languages = self::where('active', 1)->get()->toArray();
            $localizable_languages_array = [];
    
            if (count($active_languages)) {
                foreach ($active_languages as $key => $lang) {
                    $localizable_languages_array[$lang['abbr']] = $lang;
                }
    
                return $localizable_languages_array;
            }
    
            return config('laravellocalization.supportedLocales');
        }
    
        public static function findByAbbr($abbr = false)
        {
            return self::where('abbr', $abbr)->first();
        }
    }
    

    表:

    CREATE TABLE `languages` (
      `id` int(10) UNSIGNED NOT NULL,
      `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
      `app_name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
      `flag` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
      `abbr` varchar(3) COLLATE utf8_unicode_ci NOT NULL,
      `script` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
      `native` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
      `active` tinyint(3) UNSIGNED NOT NULL DEFAULT '1',
      `default` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',
      `created_at` timestamp NULL DEFAULT NULL,
      `updated_at` timestamp NULL DEFAULT NULL,
      `deleted_at` timestamp NULL DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
    

    表中数据:

    INSERT INTO `languages` (`id`, `name`, `app_name`, `flag`, `abbr`, `script`, `native`, `active`, `default`, `created_at`, `updated_at`, `deleted_at`) VALUES
    (1, 'English', 'english', '', 'en', 'Latn', 'English', 1, 0, NULL, NULL, NULL),
    (2, 'Romanian', 'romanian', '', 'ro', 'Latn', 'română', 1, 1, NULL, NULL, NULL);
    

由于我是通过一个包来完成此操作的,所以我对代码做了一些混乱。


现在,对于 spatie/laravel-可翻译包版本

  1. 在 config/app.php 中设置服务提供者,将其添加到提供者数组中:

    Spatie\Translatable\TranslatableServiceProvider::class,
    

    在模型文章中添加

    use HasTranslations;
    ,如下所示:

    use Illuminate\Database\Eloquent\Model;
    use Spatie\Translatable\HasTranslations;
    
    class NewsItem extends Model
    {
        use HasTranslations;
    
        public $translatable = ['name']; // list the columns you want to be translatable (will have json format)
    }
    
  2. 保存新条目并使用它:

    $article = new Article;
    $article->setTranslation('name', 'en', 'Updated name in English')
            ->setTranslation('name', 'nl', 'Naam in het Nederlands');
    
    $article->save();
    
    $article->name; // Returns 'Name in English' given that the current app locale is 'en'
    $article->getTranslation('name', 'nl'); // returns 'Naam in het Nederlands'
    
    app()->setLocale('nl');
    
    $article->name; // Returns 'Naam in het Nederlands'
    
  3. 示例来自:https://github.com/spatie/laravel-translatable

  4. 数据库表格式如上面第一个版本中所述

如果不起作用请告诉我,我会查看您的代码。


0
投票

我认为你应该改变你的表格结构:

帖子id,slug

post_translationsid、post_id、区域设置、标题、内容

还添加与您的帖子模型的关系:

public function translations()
{
    return $this->hasMany(PostTranslation::class, 'post_id');
}

并更新您的控制器:

$post = new Post;

$post->slug = $request->slug;
$post->save();

$post->translations()->create([
    'locale' => 'en', //or grab it from $request
    'title' => $request->title,
    'content' => $request->content
])

它将创建您的帖子并为其添加翻译

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.