@@ -8,131 +8,131 @@ |
||
| 8 | 8 | |
| 9 | 9 | trait Translatable |
| 10 | 10 | { |
| 11 | - public static $transtableFieldName = 'translatable'; |
|
| 11 | + public static $transtableFieldName = 'translatable'; |
|
| 12 | 12 | |
| 13 | - public $translatable; |
|
| 14 | - public $model; |
|
| 13 | + public $translatable; |
|
| 14 | + public $model; |
|
| 15 | 15 | |
| 16 | - protected $locale; |
|
| 16 | + protected $locale; |
|
| 17 | 17 | |
| 18 | - public static function bootTranslatable() |
|
| 19 | - { |
|
| 20 | - static::addGlobalScope(new TranslatableScope); |
|
| 18 | + public static function bootTranslatable() |
|
| 19 | + { |
|
| 20 | + static::addGlobalScope(new TranslatableScope); |
|
| 21 | 21 | |
| 22 | - static::saving(function ($model) { |
|
| 23 | - $model->translatable = collect($model->attributes)->only(static::$transtableFieldName)->toArray(); |
|
| 24 | - $model->attributes = collect($model->attributes)->except(static::$transtableFieldName)->toArray(); |
|
| 25 | - }); |
|
| 22 | + static::saving(function ($model) { |
|
| 23 | + $model->translatable = collect($model->attributes)->only(static::$transtableFieldName)->toArray(); |
|
| 24 | + $model->attributes = collect($model->attributes)->except(static::$transtableFieldName)->toArray(); |
|
| 25 | + }); |
|
| 26 | 26 | |
| 27 | - static::saved(function ($model) { |
|
| 28 | - if ($model->translatable) { |
|
| 29 | - (new self)->saveTranslation($model); |
|
| 30 | - } |
|
| 31 | - }); |
|
| 27 | + static::saved(function ($model) { |
|
| 28 | + if ($model->translatable) { |
|
| 29 | + (new self)->saveTranslation($model); |
|
| 30 | + } |
|
| 31 | + }); |
|
| 32 | 32 | |
| 33 | - static::deleted(function ($model) { |
|
| 34 | - if ((new $model)->has('translations')) { |
|
| 35 | - $model->translations()->delete(); |
|
| 36 | - } |
|
| 37 | - }); |
|
| 38 | - } |
|
| 33 | + static::deleted(function ($model) { |
|
| 34 | + if ((new $model)->has('translations')) { |
|
| 35 | + $model->translations()->delete(); |
|
| 36 | + } |
|
| 37 | + }); |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - public function locale($value = null) |
|
| 41 | - { |
|
| 42 | - $this->locale = $value; |
|
| 40 | + public function locale($value = null) |
|
| 41 | + { |
|
| 42 | + $this->locale = $value; |
|
| 43 | 43 | |
| 44 | - return $this; |
|
| 45 | - } |
|
| 44 | + return $this; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - // Relationship |
|
| 48 | - public function translations() |
|
| 49 | - { |
|
| 50 | - return $this->morphMany(Translation::class, 'translatable'); |
|
| 51 | - } |
|
| 47 | + // Relationship |
|
| 48 | + public function translations() |
|
| 49 | + { |
|
| 50 | + return $this->morphMany(Translation::class, 'translatable'); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - // scope |
|
| 54 | - public function scopeTranslated($query, ...$fields) |
|
| 55 | - { |
|
| 56 | - $this->locale = $this->locale ?? app()->getLocale(); |
|
| 53 | + // scope |
|
| 54 | + public function scopeTranslated($query, ...$fields) |
|
| 55 | + { |
|
| 56 | + $this->locale = $this->locale ?? app()->getLocale(); |
|
| 57 | 57 | |
| 58 | - $fields = empty($fields) ? $this->getTranslatedFieldsForCurrentModel() : collect($fields); |
|
| 58 | + $fields = empty($fields) ? $this->getTranslatedFieldsForCurrentModel() : collect($fields); |
|
| 59 | 59 | |
| 60 | - if ($fields->isEmpty()) { |
|
| 61 | - return $query; |
|
| 62 | - } |
|
| 60 | + if ($fields->isEmpty()) { |
|
| 61 | + return $query; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - // Add select * if nothing has been selected yet |
|
| 65 | - if ($query->getQuery()->columns === null) { |
|
| 66 | - $query->select('*'); |
|
| 67 | - } |
|
| 64 | + // Add select * if nothing has been selected yet |
|
| 65 | + if ($query->getQuery()->columns === null) { |
|
| 66 | + $query->select('*'); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - // Build the sub select |
|
| 70 | - $fields->each(function ($key) use ($query) { |
|
| 71 | - $this->subSelectTranslation($query, $key); |
|
| 72 | - }); |
|
| 69 | + // Build the sub select |
|
| 70 | + $fields->each(function ($key) use ($query) { |
|
| 71 | + $this->subSelectTranslation($query, $key); |
|
| 72 | + }); |
|
| 73 | 73 | |
| 74 | - return $query; |
|
| 75 | - } |
|
| 74 | + return $query; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - protected function subSelectTranslation($query, $key): void |
|
| 78 | - { |
|
| 79 | - $query->addSelect([ |
|
| 80 | - $key => function ($query) use ($key) { |
|
| 81 | - $query->select(Translation::getTableName() . '.value') |
|
| 82 | - ->from(Translation::getTableName()) |
|
| 83 | - ->where(Translation::getTableName() . '.translatable_type', '=', \get_class($this)) |
|
| 84 | - ->where(Translation::getTableName() . '.locale', '=', $this->locale) |
|
| 85 | - ->where(Translation::getTableName() . '.key', '=', $key) |
|
| 86 | - ->where(Translation::getTableName() . '.translatable_id', '=', \DB::raw($this->getTable() . '.' . $this->primaryKey)); |
|
| 87 | - } |
|
| 88 | - ]); |
|
| 89 | - } |
|
| 77 | + protected function subSelectTranslation($query, $key): void |
|
| 78 | + { |
|
| 79 | + $query->addSelect([ |
|
| 80 | + $key => function ($query) use ($key) { |
|
| 81 | + $query->select(Translation::getTableName() . '.value') |
|
| 82 | + ->from(Translation::getTableName()) |
|
| 83 | + ->where(Translation::getTableName() . '.translatable_type', '=', \get_class($this)) |
|
| 84 | + ->where(Translation::getTableName() . '.locale', '=', $this->locale) |
|
| 85 | + ->where(Translation::getTableName() . '.key', '=', $key) |
|
| 86 | + ->where(Translation::getTableName() . '.translatable_id', '=', \DB::raw($this->getTable() . '.' . $this->primaryKey)); |
|
| 87 | + } |
|
| 88 | + ]); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Query the translations table for possible keys if none are provided. |
|
| 93 | - * @return Collection |
|
| 94 | - */ |
|
| 95 | - public function getTranslatedFieldsForCurrentModel(): Collection |
|
| 96 | - { |
|
| 97 | - $table = Translation::getTableName(); |
|
| 91 | + /** |
|
| 92 | + * Query the translations table for possible keys if none are provided. |
|
| 93 | + * @return Collection |
|
| 94 | + */ |
|
| 95 | + public function getTranslatedFieldsForCurrentModel(): Collection |
|
| 96 | + { |
|
| 97 | + $table = Translation::getTableName(); |
|
| 98 | 98 | |
| 99 | - return DB::table($table)->select($table . '.key') |
|
| 100 | - ->where($table . '.translatable_type', \get_class($this)) |
|
| 101 | - ->where($table . '.locale', $this->locale) |
|
| 102 | - ->groupBy($table . '.key') |
|
| 103 | - ->pluck('key'); |
|
| 104 | - } |
|
| 99 | + return DB::table($table)->select($table . '.key') |
|
| 100 | + ->where($table . '.translatable_type', \get_class($this)) |
|
| 101 | + ->where($table . '.locale', $this->locale) |
|
| 102 | + ->groupBy($table . '.key') |
|
| 103 | + ->pluck('key'); |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - public function saveTranslation($model): int |
|
| 107 | - { |
|
| 108 | - return (new Translation)->store([ |
|
| 109 | - 'translatable' => $model->translatable[static::$transtableFieldName], |
|
| 110 | - 'translatable_type' => \get_class($model), |
|
| 111 | - 'translatable_id' => $model->id, |
|
| 112 | - ]); |
|
| 113 | - } |
|
| 106 | + public function saveTranslation($model): int |
|
| 107 | + { |
|
| 108 | + return (new Translation)->store([ |
|
| 109 | + 'translatable' => $model->translatable[static::$transtableFieldName], |
|
| 110 | + 'translatable_type' => \get_class($model), |
|
| 111 | + 'translatable_id' => $model->id, |
|
| 112 | + ]); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - protected function fullTextWildcards($term) |
|
| 116 | - { |
|
| 117 | - return collect(explode(' ', str_replace(['-', '+', '<', '>', '@', '(', ')', '~'], '', $term)))->map(function ($word, $key) { |
|
| 118 | - return strlen(trim($word)) >= 3 ? '+' . trim($word) . '*' : ''; |
|
| 119 | - })->implode(' '); |
|
| 120 | - } |
|
| 115 | + protected function fullTextWildcards($term) |
|
| 116 | + { |
|
| 117 | + return collect(explode(' ', str_replace(['-', '+', '<', '>', '@', '(', ')', '~'], '', $term)))->map(function ($word, $key) { |
|
| 118 | + return strlen(trim($word)) >= 3 ? '+' . trim($word) . '*' : ''; |
|
| 119 | + })->implode(' '); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - public function scopeSearchFullText($query, $term) |
|
| 123 | - { |
|
| 124 | - $term = $this->fullTextWildcards($term); |
|
| 122 | + public function scopeSearchFullText($query, $term) |
|
| 123 | + { |
|
| 124 | + $term = $this->fullTextWildcards($term); |
|
| 125 | 125 | |
| 126 | - $matches = Translation::selectRaw('translatable_id, MATCH(`value`) AGAINST (\'' . $term . '\' IN BOOLEAN MODE) as relevance') |
|
| 127 | - ->whereRaw(\DB::raw('MATCH (`value`) AGAINST (\'' . $term . '\' IN BOOLEAN MODE)')) |
|
| 128 | - ->where('translations.translatable_type', '=', \get_class($this)) |
|
| 129 | - ->having('relevance', '>', 0) |
|
| 130 | - ->orderBy('relevance') |
|
| 131 | - // force to search only in the current language |
|
| 132 | - //->where('translations.locale', app()->getLocale()); |
|
| 133 | - ->pluck('translatable_id') |
|
| 134 | - ->toArray(); |
|
| 126 | + $matches = Translation::selectRaw('translatable_id, MATCH(`value`) AGAINST (\'' . $term . '\' IN BOOLEAN MODE) as relevance') |
|
| 127 | + ->whereRaw(\DB::raw('MATCH (`value`) AGAINST (\'' . $term . '\' IN BOOLEAN MODE)')) |
|
| 128 | + ->where('translations.translatable_type', '=', \get_class($this)) |
|
| 129 | + ->having('relevance', '>', 0) |
|
| 130 | + ->orderBy('relevance') |
|
| 131 | + // force to search only in the current language |
|
| 132 | + //->where('translations.locale', app()->getLocale()); |
|
| 133 | + ->pluck('translatable_id') |
|
| 134 | + ->toArray(); |
|
| 135 | 135 | |
| 136 | - return $query->whereIn(DB::raw($this->getTable() . '.' . $this->primaryKey), $matches); |
|
| 137 | - } |
|
| 136 | + return $query->whereIn(DB::raw($this->getTable() . '.' . $this->primaryKey), $matches); |
|
| 137 | + } |
|
| 138 | 138 | } |
@@ -19,18 +19,18 @@ discard block |
||
| 19 | 19 | { |
| 20 | 20 | static::addGlobalScope(new TranslatableScope); |
| 21 | 21 | |
| 22 | - static::saving(function ($model) { |
|
| 22 | + static::saving(function($model) { |
|
| 23 | 23 | $model->translatable = collect($model->attributes)->only(static::$transtableFieldName)->toArray(); |
| 24 | 24 | $model->attributes = collect($model->attributes)->except(static::$transtableFieldName)->toArray(); |
| 25 | 25 | }); |
| 26 | 26 | |
| 27 | - static::saved(function ($model) { |
|
| 27 | + static::saved(function($model) { |
|
| 28 | 28 | if ($model->translatable) { |
| 29 | 29 | (new self)->saveTranslation($model); |
| 30 | 30 | } |
| 31 | 31 | }); |
| 32 | 32 | |
| 33 | - static::deleted(function ($model) { |
|
| 33 | + static::deleted(function($model) { |
|
| 34 | 34 | if ((new $model)->has('translations')) { |
| 35 | 35 | $model->translations()->delete(); |
| 36 | 36 | } |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | // Build the sub select |
| 70 | - $fields->each(function ($key) use ($query) { |
|
| 70 | + $fields->each(function($key) use ($query) { |
|
| 71 | 71 | $this->subSelectTranslation($query, $key); |
| 72 | 72 | }); |
| 73 | 73 | |
@@ -77,13 +77,13 @@ discard block |
||
| 77 | 77 | protected function subSelectTranslation($query, $key): void |
| 78 | 78 | { |
| 79 | 79 | $query->addSelect([ |
| 80 | - $key => function ($query) use ($key) { |
|
| 81 | - $query->select(Translation::getTableName() . '.value') |
|
| 80 | + $key => function($query) use ($key) { |
|
| 81 | + $query->select(Translation::getTableName().'.value') |
|
| 82 | 82 | ->from(Translation::getTableName()) |
| 83 | - ->where(Translation::getTableName() . '.translatable_type', '=', \get_class($this)) |
|
| 84 | - ->where(Translation::getTableName() . '.locale', '=', $this->locale) |
|
| 85 | - ->where(Translation::getTableName() . '.key', '=', $key) |
|
| 86 | - ->where(Translation::getTableName() . '.translatable_id', '=', \DB::raw($this->getTable() . '.' . $this->primaryKey)); |
|
| 83 | + ->where(Translation::getTableName().'.translatable_type', '=', \get_class($this)) |
|
| 84 | + ->where(Translation::getTableName().'.locale', '=', $this->locale) |
|
| 85 | + ->where(Translation::getTableName().'.key', '=', $key) |
|
| 86 | + ->where(Translation::getTableName().'.translatable_id', '=', \DB::raw($this->getTable().'.'.$this->primaryKey)); |
|
| 87 | 87 | } |
| 88 | 88 | ]); |
| 89 | 89 | } |
@@ -96,10 +96,10 @@ discard block |
||
| 96 | 96 | { |
| 97 | 97 | $table = Translation::getTableName(); |
| 98 | 98 | |
| 99 | - return DB::table($table)->select($table . '.key') |
|
| 100 | - ->where($table . '.translatable_type', \get_class($this)) |
|
| 101 | - ->where($table . '.locale', $this->locale) |
|
| 102 | - ->groupBy($table . '.key') |
|
| 99 | + return DB::table($table)->select($table.'.key') |
|
| 100 | + ->where($table.'.translatable_type', \get_class($this)) |
|
| 101 | + ->where($table.'.locale', $this->locale) |
|
| 102 | + ->groupBy($table.'.key') |
|
| 103 | 103 | ->pluck('key'); |
| 104 | 104 | } |
| 105 | 105 | |
@@ -114,8 +114,8 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | protected function fullTextWildcards($term) |
| 116 | 116 | { |
| 117 | - return collect(explode(' ', str_replace(['-', '+', '<', '>', '@', '(', ')', '~'], '', $term)))->map(function ($word, $key) { |
|
| 118 | - return strlen(trim($word)) >= 3 ? '+' . trim($word) . '*' : ''; |
|
| 117 | + return collect(explode(' ', str_replace(['-', '+', '<', '>', '@', '(', ')', '~'], '', $term)))->map(function($word, $key) { |
|
| 118 | + return strlen(trim($word)) >= 3 ? '+'.trim($word).'*' : ''; |
|
| 119 | 119 | })->implode(' '); |
| 120 | 120 | } |
| 121 | 121 | |
@@ -123,8 +123,8 @@ discard block |
||
| 123 | 123 | { |
| 124 | 124 | $term = $this->fullTextWildcards($term); |
| 125 | 125 | |
| 126 | - $matches = Translation::selectRaw('translatable_id, MATCH(`value`) AGAINST (\'' . $term . '\' IN BOOLEAN MODE) as relevance') |
|
| 127 | - ->whereRaw(\DB::raw('MATCH (`value`) AGAINST (\'' . $term . '\' IN BOOLEAN MODE)')) |
|
| 126 | + $matches = Translation::selectRaw('translatable_id, MATCH(`value`) AGAINST (\''.$term.'\' IN BOOLEAN MODE) as relevance') |
|
| 127 | + ->whereRaw(\DB::raw('MATCH (`value`) AGAINST (\''.$term.'\' IN BOOLEAN MODE)')) |
|
| 128 | 128 | ->where('translations.translatable_type', '=', \get_class($this)) |
| 129 | 129 | ->having('relevance', '>', 0) |
| 130 | 130 | ->orderBy('relevance') |
@@ -133,6 +133,6 @@ discard block |
||
| 133 | 133 | ->pluck('translatable_id') |
| 134 | 134 | ->toArray(); |
| 135 | 135 | |
| 136 | - return $query->whereIn(DB::raw($this->getTable() . '.' . $this->primaryKey), $matches); |
|
| 136 | + return $query->whereIn(DB::raw($this->getTable().'.'.$this->primaryKey), $matches); |
|
| 137 | 137 | } |
| 138 | 138 | } |