despark/laravel-db-i18n

Database localization package for laravel framework

v2.0 2019-01-24 08:04 UTC

This package is auto-updated.

Last update: 2024-11-06 20:04:59 UTC


README

Latest Stable Version

Despark's igniCMS DB Localization Module

About

This package extends despark/igni-core by adding a fully functional DB Localization Module.

Installation

Require using Composer

composer require despark/laravel-db-i18n

Note: despark/igni-core comes out of the box with this module.

Example usage

config/ignicms.php

   ...
    'languages' => [
        // Add languages that you will use in your app.
        [
            'locale' => 'en',
            'name' => 'English',
        ],
        [
            'locale' => 'de',
            'name' => 'Deutsche',
        ],
        [
            'locale' => 'fr',
            'name' => 'Français',
        ],
    ],
   ...

database/migrations/create_articles_table.php

 ...
  /**
   * Run the migrations.
   *
   * @return void
   */    
  public function up()
  {
      Schema::create('articles', function (Blueprint $table) {
          $table->increments('id');
          $table->string('url');
          $table->timestamps();
      });
  }

  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
      Schema::dropIfExists('articles');
  }
 ...

database/migrations/create_articles_i18n_table.php

 ...
  /**
   * Run the migrations.
   *
   * @return void
   */    
  public function up()
  {
      Schema::create('articles_i18n', function (Blueprint $table) {
          $table->increments('id');
          $table->unsignedInteger('parent_id');
          $table->string('locale');
          $table->string('title');
          $table->text('content')->nullable();
          $table->timestamps();

          $table->foreign('parent_id')
                 ->references('id')
                 ->on('articles')
                 ->onDelete('cascade')
                 ->onUpdate('cascade');
      });
  }

  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
      Schema::dropIfExists('articles_i18n');
  }
 ...

App\Models\Article.php

 ...    
  use Despark\Cms\Models\AdminModel;
  use Despark\LaravelDbLocalization\Contracts\Translatable;
  use Despark\LaravelDbLocalization\Traits\HasTranslation;

  class Article extends AdminModel implements Translatable
  {
      use HasTranslation;

      protected $table = 'articles';

      protected $translatable = [
          'title',
          'content',
      ];

      protected $fillable = [
          'title',
          'content',
          'url',
      ];

      protected $rules = [
          'title' => 'required',
          'content' => 'required',
          'url' => 'required',
      ];

      protected $identifier = 'articles';
   }
 ...

App\Http\Controllers\Admin\ArticlesController.php

...    
 use Despark\Cms\Http\Controllers\AdminController;

 class ArticlesController extends AdminController
 {
 }
...

config\entities\articles.php

   return [
        'name' => 'Articles',
        'description' => 'Articles resource',
        'model' => App\Models\Article::class,
        'controller' => App\Http\Controllers\Admin\ArticlesController::class,
        'adminColumns' => [
            'title' => 'translation.title',
            'created at' => 'created_at',
        ],
        'actions' => ['edit', 'create', 'destroy'],
        'adminFormFields' => [
            'title' => [
                'type' => 'text',
                'label' => 'Title',
            ],
            'content' => [
                'type' => 'textarea',
                'label' => 'Content',
            ],
            'url' => [
                'type' => 'text',
                'label' => 'Url',
            ],
        ],
        'adminMenu' => [
            'articles' => [
                'name' => 'Articles',
                'iconClass' => 'fa-newspaper-o',
                'link' => 'articles.index',
            ],
        ],
    ];