despark / laravel-db-i18n by lsimeonov

Database localization package for laravel framework
859
1
4
Package Data
Maintainer Username: lsimeonov
Maintainer Contact: lsimeonov@despark.com (Ludmil Simeonov)
Package Create Date: 2017-07-12
Package Last Update: 2019-01-24
Language: PHP
License: MIT
Last Refreshed: 2024-04-18 15:11:35
Package Statistics
Total Downloads: 859
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 4
Total Forks: 0
Total Open Issues: 0

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',
            ],
        ],
    ];