@@ -13,7 +13,7 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | public function up() |
| 15 | 15 | { |
| 16 | - Schema::create('translations', function (Blueprint $table) { |
|
| 16 | + Schema::create('translations', function(Blueprint $table) { |
|
| 17 | 17 | $table->string('key'); |
| 18 | 18 | $table->text('value'); |
| 19 | 19 | $table->bigInteger('translatable_id')->unsigned(); |
@@ -22,8 +22,8 @@ discard block |
||
| 22 | 22 | { |
| 23 | 23 | $this->deleteTranslation($attributes['translatable_type'], $attributes['translatable_id']); |
| 24 | 24 | |
| 25 | - collect($attributes['translatable'])->each(function ($data, $locale) use ($attributes) { |
|
| 26 | - collect($data)->each(function ($value, $key) use ($attributes, $locale) { |
|
| 25 | + collect($attributes['translatable'])->each(function($data, $locale) use ($attributes) { |
|
| 26 | + collect($data)->each(function($value, $key) use ($attributes, $locale) { |
|
| 27 | 27 | self::insert([ |
| 28 | 28 | 'key' => $key, |
| 29 | 29 | 'value' => $value ?? '', |
@@ -58,11 +58,11 @@ discard block |
||
| 58 | 58 | public function scopeOrderTranslationByKey($query, $key = 'name', $order = 'asc') |
| 59 | 59 | { |
| 60 | 60 | return $query->select(\DB::raw(' |
| 61 | - IF(translations.`key` = "' . $key . '", translations.value, "") as ' . $key . ' |
|
| 61 | + IF(translations.`key` = "' . $key.'", translations.value, "") as '.$key.' |
|
| 62 | 62 | '))->where( |
| 63 | 63 | 'translations.locale', '=', app()->getLocale() |
| 64 | 64 | )->orderBy( |
| 65 | - \DB::raw($key . ' ' . $order) |
|
| 65 | + \DB::raw($key.' '.$order) |
|
| 66 | 66 | )->groupBy('translations.translatable_id'); |
| 67 | 67 | } |
| 68 | 68 | |
@@ -15,17 +15,17 @@ |
||
| 15 | 15 | { |
| 16 | 16 | public function boot() |
| 17 | 17 | { |
| 18 | - $this->loadMigrationsFrom(__DIR__ . '/database/migrations/'); |
|
| 18 | + $this->loadMigrationsFrom(__DIR__.'/database/migrations/'); |
|
| 19 | 19 | |
| 20 | 20 | $this->publishes([ |
| 21 | - __DIR__ . '/database/migrations/' => database_path('migrations') |
|
| 21 | + __DIR__.'/database/migrations/' => database_path('migrations') |
|
| 22 | 22 | ], 'migrations'); |
| 23 | 23 | |
| 24 | 24 | $this->publishes([ |
| 25 | - __DIR__ . '/config/languages.php' => config_path('languages.php'), |
|
| 25 | + __DIR__.'/config/languages.php' => config_path('languages.php'), |
|
| 26 | 26 | ], 'config'); |
| 27 | 27 | |
| 28 | - Collection::macro('for', function ($field, $code) { |
|
| 28 | + Collection::macro('for', function($field, $code) { |
|
| 29 | 29 | return $this->where('key', $field)->where('locale', $code)->pluck('value')->first() ?? null; |
| 30 | 30 | }); |
| 31 | 31 | } |
@@ -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 | |
@@ -106,10 +106,10 @@ discard block |
||
| 106 | 106 | { |
| 107 | 107 | $table = Translation::getTableName(); |
| 108 | 108 | |
| 109 | - return DB::table($table)->select($table . '.key') |
|
| 110 | - ->where($table . '.translatable_type', \get_class($this)) |
|
| 111 | - ->where($table . '.locale', $this->locale) |
|
| 112 | - ->groupBy($table . '.key') |
|
| 109 | + return DB::table($table)->select($table.'.key') |
|
| 110 | + ->where($table.'.translatable_type', \get_class($this)) |
|
| 111 | + ->where($table.'.locale', $this->locale) |
|
| 112 | + ->groupBy($table.'.key') |
|
| 113 | 113 | ->pluck('key'); |
| 114 | 114 | } |
| 115 | 115 | |
@@ -124,20 +124,20 @@ discard block |
||
| 124 | 124 | |
| 125 | 125 | protected function fullTextWildcards($term) |
| 126 | 126 | { |
| 127 | - return collect(explode(' ', str_replace(['-', '+', '<', '>', '@', '(', ')', '~'], '', $term)))->map(function ($word, $key) { |
|
| 128 | - return strlen($word) >= 3 ? '+' . $word . '*' : ''; |
|
| 127 | + return collect(explode(' ', str_replace(['-', '+', '<', '>', '@', '(', ')', '~'], '', $term)))->map(function($word, $key) { |
|
| 128 | + return strlen($word) >= 3 ? '+'.$word.'*' : ''; |
|
| 129 | 129 | })->implode(' '); |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | public function scopeSearchFullText($query, $term, $fields = []) |
| 133 | 133 | { |
| 134 | 134 | $alias = Str::random(8); |
| 135 | - return $query->join(\DB::raw('translations as ' . $alias . ' ON ' . $alias . '.translatable_id = ' . $this->getTable() . '.' . $this->primaryKey)) |
|
| 135 | + return $query->join(\DB::raw('translations as '.$alias.' ON '.$alias.'.translatable_id = '.$this->getTable().'.'.$this->primaryKey)) |
|
| 136 | 136 | ->addSelect([ |
| 137 | - 'relevance' => function ($query) use ($term, $alias) { |
|
| 138 | - $query->selectRaw("MATCH (`' . $alias . '`.`value`) AGAINST ('" . $this->fullTextWildcards($term) . "' IN BOOLEAN MODE)") |
|
| 137 | + 'relevance' => function($query) use ($term, $alias) { |
|
| 138 | + $query->selectRaw("MATCH (`' . $alias . '`.`value`) AGAINST ('".$this->fullTextWildcards($term)."' IN BOOLEAN MODE)") |
|
| 139 | 139 | ->from('translations') |
| 140 | - ->where($alias . '.translatable_id', DB::raw($this->getTable() . '.' . $this->primaryKey)) |
|
| 140 | + ->where($alias.'.translatable_id', DB::raw($this->getTable().'.'.$this->primaryKey)) |
|
| 141 | 141 | ->limit(1); |
| 142 | 142 | } |
| 143 | 143 | ]) |