Storing 2 same id of post table with different language into database

I'm building a multi language website. In this case just 2 languages, it's Indonesian and English using Laravel. I have posts table, it will store id of each post and post_translations table is to store local, title, and description of post. I got the problem when storing data into database. I don't have any idea how to store post without inc the id except I've added 2 same post with Indonesian and English.

This is the result (wrong)

posts table

id 
1
2

post_translations table

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

Expexted result

posts table

id 
1

post_translations table

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

PostController

  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');
}

Ok, so here we go (please note that this is not the only way):

  • install spatie/laravel-translatable with

    composer require spatie/laravel-translatable

  • **Note: for native spatie/laravel-translatable go to version 2 **

  • create a table with this structure:

    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;

  • Note: beter use a migration. I just exported a table I did earlier to test

  • Insert the data in the datatabase in json format like this:
  •     INSERT INTO `pages` (`id`, `title`, `slug`, `content`, `created_at`, `updated_at`) VALUES
            (1, '{"ro":"Acasu0103","en":"Home"}', NULL, '{"ro":"<p><strong>Test title</strong></p>rnrn<p>Test content romanian</p>rn","en":"<p><strong>test title english</strong></p>rnrn<p>test content english.</p>rn"}', '2017-04-03 11:45:56', '2017-04-03 12:15:16');

    I think you should change your tables strucuture:

    posts : id, slug.

    post_translations : id, post_id, locale, title, content

    Also add relation to your Post model:

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

    And update your controller:

    $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
    ])
    

    It will create your post and add translation to it

    链接地址: http://www.djcxy.com/p/37512.html

    上一篇: 你如何组织你的版本控制库?

    下一篇: 将具有不同语言的2张相同的帖子表存储到数据库中