@@ -7,75 +7,75 @@ discard block |
||
7 | 7 | |
8 | 8 | trait Translatable |
9 | 9 | { |
10 | - public static $transtableFieldName = 'translatable'; |
|
10 | + public static $transtableFieldName = 'translatable'; |
|
11 | 11 | |
12 | - public $translatable; |
|
13 | - public $model; |
|
12 | + public $translatable; |
|
13 | + public $model; |
|
14 | 14 | |
15 | - protected $locale; |
|
15 | + protected $locale; |
|
16 | 16 | |
17 | - public static function bootTranslatable() |
|
18 | - { |
|
19 | - static::addGlobalScope(new TranslatableScope); |
|
17 | + public static function bootTranslatable() |
|
18 | + { |
|
19 | + static::addGlobalScope(new TranslatableScope); |
|
20 | 20 | |
21 | - static::saving(function ($model) { |
|
22 | - $model->translatable = collect($model->attributes)->only(static::$transtableFieldName)->toArray(); |
|
23 | - $model->attributes = collect($model->attributes)->except(static::$transtableFieldName)->toArray(); |
|
24 | - }); |
|
21 | + static::saving(function ($model) { |
|
22 | + $model->translatable = collect($model->attributes)->only(static::$transtableFieldName)->toArray(); |
|
23 | + $model->attributes = collect($model->attributes)->except(static::$transtableFieldName)->toArray(); |
|
24 | + }); |
|
25 | 25 | |
26 | - static::saved(function ($model) { |
|
27 | - if ($model->translatable) { |
|
28 | - (new self)->saveTranslation($model); |
|
29 | - } |
|
30 | - }); |
|
26 | + static::saved(function ($model) { |
|
27 | + if ($model->translatable) { |
|
28 | + (new self)->saveTranslation($model); |
|
29 | + } |
|
30 | + }); |
|
31 | 31 | |
32 | - static::deleted(function ($model) { |
|
33 | - if ((new $model)->has('translations')) { |
|
34 | - $model->translations()->delete(); |
|
35 | - } |
|
36 | - }); |
|
37 | - } |
|
32 | + static::deleted(function ($model) { |
|
33 | + if ((new $model)->has('translations')) { |
|
34 | + $model->translations()->delete(); |
|
35 | + } |
|
36 | + }); |
|
37 | + } |
|
38 | 38 | |
39 | - public function locale($value = null) |
|
40 | - { |
|
41 | - $this->locale = $value; |
|
39 | + public function locale($value = null) |
|
40 | + { |
|
41 | + $this->locale = $value; |
|
42 | 42 | |
43 | - return $this; |
|
44 | - } |
|
43 | + return $this; |
|
44 | + } |
|
45 | 45 | |
46 | - // Relationship |
|
47 | - public function translations() |
|
48 | - { |
|
49 | - return $this->morphMany(Translation::class, 'translatable'); |
|
50 | - } |
|
46 | + // Relationship |
|
47 | + public function translations() |
|
48 | + { |
|
49 | + return $this->morphMany(Translation::class, 'translatable'); |
|
50 | + } |
|
51 | 51 | |
52 | - // scope |
|
53 | - public function scopeTranslated($query, ...$fields) |
|
54 | - { |
|
55 | - $this->locale = $this->locale ?? app()->getLocale(); |
|
52 | + // scope |
|
53 | + public function scopeTranslated($query, ...$fields) |
|
54 | + { |
|
55 | + $this->locale = $this->locale ?? app()->getLocale(); |
|
56 | 56 | |
57 | - $fields = empty($fields) ? $this->getTranslatedFieldsForCurrentModel() : collect($fields); |
|
57 | + $fields = empty($fields) ? $this->getTranslatedFieldsForCurrentModel() : collect($fields); |
|
58 | 58 | |
59 | - if ($fields->isEmpty()) { |
|
60 | - return $query; |
|
61 | - } |
|
59 | + if ($fields->isEmpty()) { |
|
60 | + return $query; |
|
61 | + } |
|
62 | 62 | |
63 | - // Add select * if nothing has been selected yet |
|
64 | - if ($query->getQuery()->columns === null) { |
|
65 | - $query->select('*'); |
|
66 | - } |
|
63 | + // Add select * if nothing has been selected yet |
|
64 | + if ($query->getQuery()->columns === null) { |
|
65 | + $query->select('*'); |
|
66 | + } |
|
67 | 67 | |
68 | - // Build the sub select |
|
69 | - $fields->each(function ($key) use ($query) { |
|
70 | - $this->subSelectTranslation($query, $key); |
|
71 | - }); |
|
68 | + // Build the sub select |
|
69 | + $fields->each(function ($key) use ($query) { |
|
70 | + $this->subSelectTranslation($query, $key); |
|
71 | + }); |
|
72 | 72 | |
73 | - return $query; |
|
74 | - } |
|
73 | + return $query; |
|
74 | + } |
|
75 | 75 | |
76 | - protected function subSelectTranslation($query, $key): void |
|
77 | - { |
|
78 | - $query->addSelect(\DB::raw('( |
|
76 | + protected function subSelectTranslation($query, $key): void |
|
77 | + { |
|
78 | + $query->addSelect(\DB::raw('( |
|
79 | 79 | SELECT `' . Translation::getTableName() . '`.`value` |
80 | 80 | FROM `' . Translation::getTableName() . '` |
81 | 81 | WHERE `' . Translation::getTableName() . '`.`translatable_type` = "' . \get_class($this) . '" |
@@ -83,30 +83,30 @@ discard block |
||
83 | 83 | AND `' . Translation::getTableName() . '`.`key` = "' . $key . '" |
84 | 84 | AND `' . Translation::getTableName() . '`.`translatable_id` = `' . $this->getTable() . '`.`' . $this->primaryKey . '` |
85 | 85 | ) as `' . $key . '`') |
86 | - ); |
|
87 | - } |
|
86 | + ); |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Query the translations table for possible keys if none are provided. |
|
91 | - * @return Collection |
|
92 | - */ |
|
93 | - public function getTranslatedFieldsForCurrentModel(): Collection |
|
94 | - { |
|
95 | - $table = Translation::getTableName(); |
|
89 | + /** |
|
90 | + * Query the translations table for possible keys if none are provided. |
|
91 | + * @return Collection |
|
92 | + */ |
|
93 | + public function getTranslatedFieldsForCurrentModel(): Collection |
|
94 | + { |
|
95 | + $table = Translation::getTableName(); |
|
96 | 96 | |
97 | - return DB::table($table)->select($table . '.key') |
|
98 | - ->where($table . '.translatable_type', \get_class($this)) |
|
99 | - ->where($table . '.locale', $this->locale) |
|
100 | - ->groupBy($table . '.key') |
|
101 | - ->pluck('key'); |
|
102 | - } |
|
97 | + return DB::table($table)->select($table . '.key') |
|
98 | + ->where($table . '.translatable_type', \get_class($this)) |
|
99 | + ->where($table . '.locale', $this->locale) |
|
100 | + ->groupBy($table . '.key') |
|
101 | + ->pluck('key'); |
|
102 | + } |
|
103 | 103 | |
104 | - public function saveTranslation($model): int |
|
105 | - { |
|
106 | - return (new Translation)->store([ |
|
107 | - 'translatable' => $model->translatable[static::$transtableFieldName], |
|
108 | - 'translatable_type' => \get_class($model), |
|
109 | - 'translatable_id' => $model->id, |
|
110 | - ]); |
|
111 | - } |
|
104 | + public function saveTranslation($model): int |
|
105 | + { |
|
106 | + return (new Translation)->store([ |
|
107 | + 'translatable' => $model->translatable[static::$transtableFieldName], |
|
108 | + 'translatable_type' => \get_class($model), |
|
109 | + 'translatable_id' => $model->id, |
|
110 | + ]); |
|
111 | + } |
|
112 | 112 | } |
@@ -18,18 +18,18 @@ discard block |
||
18 | 18 | { |
19 | 19 | static::addGlobalScope(new TranslatableScope); |
20 | 20 | |
21 | - static::saving(function ($model) { |
|
21 | + static::saving(function($model) { |
|
22 | 22 | $model->translatable = collect($model->attributes)->only(static::$transtableFieldName)->toArray(); |
23 | 23 | $model->attributes = collect($model->attributes)->except(static::$transtableFieldName)->toArray(); |
24 | 24 | }); |
25 | 25 | |
26 | - static::saved(function ($model) { |
|
26 | + static::saved(function($model) { |
|
27 | 27 | if ($model->translatable) { |
28 | 28 | (new self)->saveTranslation($model); |
29 | 29 | } |
30 | 30 | }); |
31 | 31 | |
32 | - static::deleted(function ($model) { |
|
32 | + static::deleted(function($model) { |
|
33 | 33 | if ((new $model)->has('translations')) { |
34 | 34 | $model->translations()->delete(); |
35 | 35 | } |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | } |
67 | 67 | |
68 | 68 | // Build the sub select |
69 | - $fields->each(function ($key) use ($query) { |
|
69 | + $fields->each(function($key) use ($query) { |
|
70 | 70 | $this->subSelectTranslation($query, $key); |
71 | 71 | }); |
72 | 72 | |
@@ -76,13 +76,13 @@ discard block |
||
76 | 76 | protected function subSelectTranslation($query, $key): void |
77 | 77 | { |
78 | 78 | $query->addSelect(\DB::raw('( |
79 | - SELECT `' . Translation::getTableName() . '`.`value` |
|
80 | - FROM `' . Translation::getTableName() . '` |
|
81 | - WHERE `' . Translation::getTableName() . '`.`translatable_type` = "' . \get_class($this) . '" |
|
82 | - AND `' . Translation::getTableName() . '`.`locale` = "' . $this->locale . '" |
|
83 | - AND `' . Translation::getTableName() . '`.`key` = "' . $key . '" |
|
84 | - AND `' . Translation::getTableName() . '`.`translatable_id` = `' . $this->getTable() . '`.`' . $this->primaryKey . '` |
|
85 | - ) as `' . $key . '`') |
|
79 | + SELECT `' . Translation::getTableName().'`.`value` |
|
80 | + FROM `' . Translation::getTableName().'` |
|
81 | + WHERE `' . Translation::getTableName().'`.`translatable_type` = "'.\get_class($this).'" |
|
82 | + AND `' . Translation::getTableName().'`.`locale` = "'.$this->locale.'" |
|
83 | + AND `' . Translation::getTableName().'`.`key` = "'.$key.'" |
|
84 | + AND `' . Translation::getTableName().'`.`translatable_id` = `'.$this->getTable().'`.`'.$this->primaryKey.'` |
|
85 | + ) as `' . $key.'`') |
|
86 | 86 | ); |
87 | 87 | } |
88 | 88 | |
@@ -94,10 +94,10 @@ discard block |
||
94 | 94 | { |
95 | 95 | $table = Translation::getTableName(); |
96 | 96 | |
97 | - return DB::table($table)->select($table . '.key') |
|
98 | - ->where($table . '.translatable_type', \get_class($this)) |
|
99 | - ->where($table . '.locale', $this->locale) |
|
100 | - ->groupBy($table . '.key') |
|
97 | + return DB::table($table)->select($table.'.key') |
|
98 | + ->where($table.'.translatable_type', \get_class($this)) |
|
99 | + ->where($table.'.locale', $this->locale) |
|
100 | + ->groupBy($table.'.key') |
|
101 | 101 | ->pluck('key'); |
102 | 102 | } |
103 | 103 |
@@ -8,8 +8,8 @@ |
||
8 | 8 | |
9 | 9 | class TranslatableScope implements Scope |
10 | 10 | { |
11 | - public function apply(Builder $builder, Model $model) |
|
12 | - { |
|
13 | - $builder->translated(); |
|
14 | - } |
|
11 | + public function apply(Builder $builder, Model $model) |
|
12 | + { |
|
13 | + $builder->translated(); |
|
14 | + } |
|
15 | 15 | } |
@@ -6,29 +6,29 @@ |
||
6 | 6 | |
7 | 7 | class CreateTranslationsTable extends Migration |
8 | 8 | { |
9 | - /** |
|
10 | - * Run the migrations. |
|
11 | - * |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function up() |
|
15 | - { |
|
16 | - Schema::create('translations', function (Blueprint $table) { |
|
17 | - $table->string('key'); |
|
18 | - $table->text('value'); |
|
19 | - $table->bigInteger('translatable_id')->unsigned(); |
|
20 | - $table->string('translatable_type'); |
|
21 | - $table->string('locale'); |
|
22 | - }); |
|
23 | - } |
|
9 | + /** |
|
10 | + * Run the migrations. |
|
11 | + * |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function up() |
|
15 | + { |
|
16 | + Schema::create('translations', function (Blueprint $table) { |
|
17 | + $table->string('key'); |
|
18 | + $table->text('value'); |
|
19 | + $table->bigInteger('translatable_id')->unsigned(); |
|
20 | + $table->string('translatable_type'); |
|
21 | + $table->string('locale'); |
|
22 | + }); |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * Reverse the migrations. |
|
27 | - * |
|
28 | - * @return void |
|
29 | - */ |
|
30 | - public function down() |
|
31 | - { |
|
32 | - Schema::dropIfExists('translations'); |
|
33 | - } |
|
25 | + /** |
|
26 | + * Reverse the migrations. |
|
27 | + * |
|
28 | + * @return void |
|
29 | + */ |
|
30 | + public function down() |
|
31 | + { |
|
32 | + Schema::dropIfExists('translations'); |
|
33 | + } |
|
34 | 34 | } |
@@ -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(); |
@@ -13,24 +13,24 @@ |
||
13 | 13 | |
14 | 14 | class TranslatableServiceProvider extends ServiceProvider |
15 | 15 | { |
16 | - public function boot() |
|
17 | - { |
|
18 | - $this->loadMigrationsFrom(__DIR__ . '/database/migrations/'); |
|
16 | + public function boot() |
|
17 | + { |
|
18 | + $this->loadMigrationsFrom(__DIR__ . '/database/migrations/'); |
|
19 | 19 | |
20 | - $this->publishes([ |
|
21 | - __DIR__ . '/database/migrations/' => database_path('migrations') |
|
22 | - ], 'migrations'); |
|
20 | + $this->publishes([ |
|
21 | + __DIR__ . '/database/migrations/' => database_path('migrations') |
|
22 | + ], 'migrations'); |
|
23 | 23 | |
24 | - $this->publishes([ |
|
25 | - __DIR__ . '/config/languages.php' => config_path('languages.php'), |
|
26 | - ], 'config'); |
|
24 | + $this->publishes([ |
|
25 | + __DIR__ . '/config/languages.php' => config_path('languages.php'), |
|
26 | + ], 'config'); |
|
27 | 27 | |
28 | - Collection::macro('for', function ($field, $code) { |
|
29 | - return $this->where('key', $field)->where('locale', $code)->pluck('value')->first() ?? $field; |
|
30 | - }); |
|
31 | - } |
|
28 | + Collection::macro('for', function ($field, $code) { |
|
29 | + return $this->where('key', $field)->where('locale', $code)->pluck('value')->first() ?? $field; |
|
30 | + }); |
|
31 | + } |
|
32 | 32 | |
33 | - public function register() |
|
34 | - { |
|
35 | - } |
|
33 | + public function register() |
|
34 | + { |
|
35 | + } |
|
36 | 36 | } |
@@ -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() ?? $field; |
30 | 30 | }); |
31 | 31 | } |
@@ -7,72 +7,72 @@ |
||
7 | 7 | |
8 | 8 | class Translation extends Model |
9 | 9 | { |
10 | - protected $table = 'translations'; |
|
10 | + protected $table = 'translations'; |
|
11 | 11 | |
12 | - protected $primaryKey = 'translatable_id'; |
|
12 | + protected $primaryKey = 'translatable_id'; |
|
13 | 13 | |
14 | - protected $guarded = []; |
|
14 | + protected $guarded = []; |
|
15 | 15 | |
16 | - public static function getTableName() |
|
17 | - { |
|
18 | - return (new static)->getTable(); |
|
19 | - } |
|
16 | + public static function getTableName() |
|
17 | + { |
|
18 | + return (new static)->getTable(); |
|
19 | + } |
|
20 | 20 | |
21 | - public function store(array $attributes, array $values = []): int |
|
22 | - { |
|
23 | - $this->deleteTranslation($attributes['translatable_type'], $attributes['translatable_id']); |
|
21 | + public function store(array $attributes, array $values = []): int |
|
22 | + { |
|
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) { |
|
27 | - self::insert([ |
|
28 | - 'key' => $key, |
|
29 | - 'value' => $value ?? '', |
|
30 | - 'locale' => $locale, |
|
31 | - 'translatable_type' => $attributes['translatable_type'], |
|
32 | - 'translatable_id' => $attributes['translatable_id'], |
|
33 | - ]); |
|
34 | - }); |
|
35 | - }); |
|
25 | + collect($attributes['translatable'])->each(function ($data, $locale) use ($attributes) { |
|
26 | + collect($data)->each(function ($value, $key) use ($attributes, $locale) { |
|
27 | + self::insert([ |
|
28 | + 'key' => $key, |
|
29 | + 'value' => $value ?? '', |
|
30 | + 'locale' => $locale, |
|
31 | + 'translatable_type' => $attributes['translatable_type'], |
|
32 | + 'translatable_id' => $attributes['translatable_id'], |
|
33 | + ]); |
|
34 | + }); |
|
35 | + }); |
|
36 | 36 | |
37 | - return $attributes['translatable_id']; |
|
38 | - } |
|
37 | + return $attributes['translatable_id']; |
|
38 | + } |
|
39 | 39 | |
40 | - public function deleteTranslation($type, $id) |
|
41 | - { |
|
42 | - $instance = self::where([ |
|
43 | - 'translatable_type' => $type, |
|
44 | - 'translatable_id' => $id, |
|
45 | - ]); |
|
40 | + public function deleteTranslation($type, $id) |
|
41 | + { |
|
42 | + $instance = self::where([ |
|
43 | + 'translatable_type' => $type, |
|
44 | + 'translatable_id' => $id, |
|
45 | + ]); |
|
46 | 46 | |
47 | - if ($instance) { |
|
48 | - $instance->delete(); |
|
49 | - } |
|
50 | - } |
|
47 | + if ($instance) { |
|
48 | + $instance->delete(); |
|
49 | + } |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @param QueryBuilder $query |
|
54 | - * @param string $key |
|
55 | - * @param string $order |
|
56 | - * @return Builder |
|
57 | - */ |
|
58 | - public function scopeOrderTranslationByKey($query, $key = 'name', $order = 'asc') |
|
59 | - { |
|
60 | - return $query->select(\DB::raw(' |
|
52 | + /** |
|
53 | + * @param QueryBuilder $query |
|
54 | + * @param string $key |
|
55 | + * @param string $order |
|
56 | + * @return Builder |
|
57 | + */ |
|
58 | + public function scopeOrderTranslationByKey($query, $key = 'name', $order = 'asc') |
|
59 | + { |
|
60 | + return $query->select(\DB::raw(' |
|
61 | 61 | IF(translations.`key` = "' . $key . '", translations.value, "") as ' . $key . ' |
62 | 62 | '))->where( |
63 | - 'translations.locale', '=', app()->getLocale() |
|
64 | - )->orderBy( |
|
65 | - \DB::raw($key . ' ' . $order) |
|
66 | - )->groupBy('translations.translatable_id'); |
|
67 | - } |
|
63 | + 'translations.locale', '=', app()->getLocale() |
|
64 | + )->orderBy( |
|
65 | + \DB::raw($key . ' ' . $order) |
|
66 | + )->groupBy('translations.translatable_id'); |
|
67 | + } |
|
68 | 68 | |
69 | - public function translatable() |
|
70 | - { |
|
71 | - return $this->morphTo(); |
|
72 | - } |
|
69 | + public function translatable() |
|
70 | + { |
|
71 | + return $this->morphTo(); |
|
72 | + } |
|
73 | 73 | |
74 | - public function getTranslatableAttribute() |
|
75 | - { |
|
76 | - return $this->attributes['translatable'] = request('translatable'); |
|
77 | - } |
|
74 | + public function getTranslatableAttribute() |
|
75 | + { |
|
76 | + return $this->attributes['translatable'] = request('translatable'); |
|
77 | + } |
|
78 | 78 | } |
@@ -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 |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | return [ |
4 | - 'en' => 'English', |
|
5 | - 'fr' => 'Français', |
|
4 | + 'en' => 'English', |
|
5 | + 'fr' => 'Français', |
|
6 | 6 | ]; |