Skip to content

Instantly share code, notes, and snippets.

@sajt
Last active April 26, 2022 09:24
Show Gist options
  • Select an option

  • Save sajt/0459484d339f06faa49ee93a23dbd205 to your computer and use it in GitHub Desktop.

Select an option

Save sajt/0459484d339f06faa49ee93a23dbd205 to your computer and use it in GitHub Desktop.

Revisions

  1. sajt revised this gist Apr 26, 2022. 1 changed file with 8 additions and 17 deletions.
    25 changes: 8 additions & 17 deletions TranslationTrait.php
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,12 @@
    <?php

    namespace App\Traits;

    use Illuminate\Support\Facades\Schema;

    trait TranslationTrait {
    public function __get($key) {
    $columns = Schema::getColumnListing($this->getTable());
    $translatable = [];
    foreach ($columns as $column) {
    if (str_ends_with($column, '_en')) {
    $translatable[] = substr($column, 0, -3);
    }
    }
    if (in_array($key, $translatable)) {
    return $this->{$key.'_'.app()->getLocale()};

    public function __get($key)
    {
    if (collect(Schema::getColumnListing($this->getTable()))->contains($column = $key.'_'.app()->getLocale()))
    {
    return $this->{$column};
    }

    return parent::__get($key);
    }
    }
    }
  2. sajt revised this gist Apr 25, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion TranslationTrait.php
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ public function __get($key) {
    $columns = Schema::getColumnListing($this->getTable());
    $translatable = [];
    foreach ($columns as $column) {
    if (str_ends_with('_en', $column)) {
    if (str_ends_with($column, '_en')) {
    $translatable[] = substr($column, 0, -3);
    }
    }
  3. sajt revised this gist Apr 25, 2022. No changes.
  4. sajt created this gist Apr 25, 2022.
    21 changes: 21 additions & 0 deletions TranslationTrait.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    <?php

    namespace App\Traits;

    use Illuminate\Support\Facades\Schema;

    trait TranslationTrait {
    public function __get($key) {
    $columns = Schema::getColumnListing($this->getTable());
    $translatable = [];
    foreach ($columns as $column) {
    if (str_ends_with('_en', $column)) {
    $translatable[] = substr($column, 0, -3);
    }
    }
    if (in_array($key, $translatable)) {
    return $this->{$key.'_'.app()->getLocale()};
    }
    return parent::__get($key);
    }
    }