Sky
Sky

Reputation: 4370

Laravel: How to add a word for a language in RUNTIME

Well the title is pretty clear.

I want my USERS to add custom lines in Laravel. Obviously they should not go open messages.php file and edit it :')

Imagine that user wants to create a category. But they want to name that category in several languages.

Can I do this in Laravel? if not what's the best way?

Upvotes: 3

Views: 2071

Answers (1)

Nicklas Kevin Frank
Nicklas Kevin Frank

Reputation: 6337

So what you need is for your Category model to be translatable and you want it to have multiple translations.

A common way of solving this is by moving the data you want translated out to its own table; and then having a reference to the model that the translations belongs too.


The following package: Laravel Translatable explains it very well and has a couple of very nice examples.

Below I changed the migration example to fit your needs. It should give you a general idea of how to solve this.

Schema::create('category', function(Blueprint $table)
{
    $table->increments('id');
    $table->timestamps();
});

Schema::create('category_translations', function(Blueprint $table)
{
    $table->increments('id');
    $table->integer('category_id')->unsigned();
    $table->string('name');
    $table->string('locale')->index();

    $table->unique(['category_id','locale']);
    $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});

Additionally you can find a very good article by Freek Van der Herten that explains How to add multilingual support to eloquent

Upvotes: 1

Related Questions