Completed
Push — master ( 4eda87...05e8ff )
by Mike
09:01 queued 05:03
created
src/database/migrations/2018_02_25_003452_create_translations_table.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@
 block discarded – undo
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();
Please login to merge, or discard this patch.
Indentation   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -6,35 +6,35 @@
 block discarded – undo
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
-		});
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 23
 		
24
-		// sqlite does not like this...
25
-		if (app()->environment() !== 'testing') {
26
-			\DB::statement('ALTER TABLE `translations` ADD FULLTEXT fulltext_index (`value`)');
27
-			\DB::statement('ALTER TABLE `translations` ADD INDEX `type_local_key_id`(`key`, `translatable_id`, `translatable_type`, `locale`);');
28
-		}
29
-	}
24
+        // sqlite does not like this...
25
+        if (app()->environment() !== 'testing') {
26
+            \DB::statement('ALTER TABLE `translations` ADD FULLTEXT fulltext_index (`value`)');
27
+            \DB::statement('ALTER TABLE `translations` ADD INDEX `type_local_key_id`(`key`, `translatable_id`, `translatable_type`, `locale`);');
28
+        }
29
+    }
30 30
 	
31
-	/**
32
-	 * Reverse the migrations.
33
-	 *
34
-	 * @return void
35
-	 */
36
-	public function down()
37
-	{
38
-		Schema::dropIfExists('translations');
39
-	}
31
+    /**
32
+     * Reverse the migrations.
33
+     *
34
+     * @return void
35
+     */
36
+    public function down()
37
+    {
38
+        Schema::dropIfExists('translations');
39
+    }
40 40
 }
41 41
\ No newline at end of file
Please login to merge, or discard this patch.
src/Translatable.php 2 patches
Indentation   +104 added lines, -104 removed lines patch added patch discarded remove patch
@@ -7,84 +7,84 @@  discard block
 block discarded – undo
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([
79
-			$key => function ($query) use ($key) {
80
-				$query->select(Translation::getTableName() . '.value')
81
-					->from(Translation::getTableName())
82
-					->where(Translation::getTableName() . '.translatable_type', '=', \get_class($this))
83
-					->where(Translation::getTableName() . '.locale', '=', $this->locale)
84
-					->where(Translation::getTableName() . '.key', '=', $key)
85
-					->where(Translation::getTableName() . '.translatable_id', '=', \DB::raw($this->getTable() . '.' . $this->primaryKey));
86
-			}
87
-		]);
76
+    protected function subSelectTranslation($query, $key): void
77
+    {
78
+        $query->addSelect([
79
+            $key => function ($query) use ($key) {
80
+                $query->select(Translation::getTableName() . '.value')
81
+                    ->from(Translation::getTableName())
82
+                    ->where(Translation::getTableName() . '.translatable_type', '=', \get_class($this))
83
+                    ->where(Translation::getTableName() . '.locale', '=', $this->locale)
84
+                    ->where(Translation::getTableName() . '.key', '=', $key)
85
+                    ->where(Translation::getTableName() . '.translatable_id', '=', \DB::raw($this->getTable() . '.' . $this->primaryKey));
86
+            }
87
+        ]);
88 88
 
89 89
 //        $query->addSelect(\DB::raw('(
90 90
 //			SELECT      `' . Translation::getTableName() . '`.`value`
@@ -95,51 +95,51 @@  discard block
 block discarded – undo
95 95
 //			   AND      `' . Translation::getTableName() . '`.`translatable_id` = `' . $this->getTable() . '`.`' . $this->primaryKey . '`
96 96
 //			) as `' . $key . '`')
97 97
 //        );
98
-	}
98
+    }
99 99
 	
100
-	/**
101
-	 * Query the translations table for possible keys if none are provided.
102
-	 * @return Collection
103
-	 */
104
-	public function getTranslatedFieldsForCurrentModel(): Collection
105
-	{
106
-		$table = Translation::getTableName();
100
+    /**
101
+     * Query the translations table for possible keys if none are provided.
102
+     * @return Collection
103
+     */
104
+    public function getTranslatedFieldsForCurrentModel(): Collection
105
+    {
106
+        $table = Translation::getTableName();
107 107
 		
108
-		return DB::table($table)->select($table . '.key')
109
-			->where($table . '.translatable_type', \get_class($this))
110
-			->where($table . '.locale', $this->locale)
111
-			->groupBy($table . '.key')
112
-			->pluck('key');
113
-	}
108
+        return DB::table($table)->select($table . '.key')
109
+            ->where($table . '.translatable_type', \get_class($this))
110
+            ->where($table . '.locale', $this->locale)
111
+            ->groupBy($table . '.key')
112
+            ->pluck('key');
113
+    }
114 114
 	
115
-	public function saveTranslation($model): int
116
-	{
117
-		return (new Translation)->store([
118
-			'translatable'      => $model->translatable[static::$transtableFieldName],
119
-			'translatable_type' => \get_class($model),
120
-			'translatable_id'   => $model->id,
121
-		]);
122
-	}
115
+    public function saveTranslation($model): int
116
+    {
117
+        return (new Translation)->store([
118
+            'translatable'      => $model->translatable[static::$transtableFieldName],
119
+            'translatable_type' => \get_class($model),
120
+            'translatable_id'   => $model->id,
121
+        ]);
122
+    }
123 123
 	
124
-	protected function fullTextWildcards($term)
125
-	{
126
-		return collect(explode(' ', str_replace(['-', '+', '<', '>', '@', '(', ')', '~'], '', $term)))->map(function ($word, $key) {
127
-			return strlen($word) >= 3 ? '+' . $word . '*' : '';
128
-		})->implode(' ');
129
-	}
124
+    protected function fullTextWildcards($term)
125
+    {
126
+        return collect(explode(' ', str_replace(['-', '+', '<', '>', '@', '(', ')', '~'], '', $term)))->map(function ($word, $key) {
127
+            return strlen($word) >= 3 ? '+' . $word . '*' : '';
128
+        })->implode(' ');
129
+    }
130 130
 	
131
-	public function scopeSearchFullText($query, $term)
132
-	{
133
-		return $query->join('translations', 'translations.translatable_id', '=', 'products.id')
134
-			->addSelect([
135
-				'relevance' => function ($query) use ($term) {
136
-					$query->selectRaw("MATCH (`translations`.`value`) AGAINST ('" . $this->fullTextWildcards($term) . "' IN BOOLEAN MODE)")
137
-						->from('translations')
138
-						->where('translations.translatable_id', DB::raw($this->getTable() . '.' . $this->primaryKey))
139
-						->limit(1);
140
-				}
141
-			])
142
-			->whereRaw("MATCH (`translations`.`value`) AGAINST (? IN BOOLEAN MODE)", $this->fullTextWildcards($term))
143
-			->orderByDesc('relevance');
144
-	}
131
+    public function scopeSearchFullText($query, $term)
132
+    {
133
+        return $query->join('translations', 'translations.translatable_id', '=', 'products.id')
134
+            ->addSelect([
135
+                'relevance' => function ($query) use ($term) {
136
+                    $query->selectRaw("MATCH (`translations`.`value`) AGAINST ('" . $this->fullTextWildcards($term) . "' IN BOOLEAN MODE)")
137
+                        ->from('translations')
138
+                        ->where('translations.translatable_id', DB::raw($this->getTable() . '.' . $this->primaryKey))
139
+                        ->limit(1);
140
+                }
141
+            ])
142
+            ->whereRaw("MATCH (`translations`.`value`) AGAINST (? IN BOOLEAN MODE)", $this->fullTextWildcards($term))
143
+            ->orderByDesc('relevance');
144
+    }
145 145
 }
Please login to merge, or discard this patch.
Spacing   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -18,18 +18,18 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
76 76
 	protected function subSelectTranslation($query, $key): void
77 77
 	{
78 78
 		$query->addSelect([
79
-			$key => function ($query) use ($key) {
80
-				$query->select(Translation::getTableName() . '.value')
79
+			$key => function($query) use ($key) {
80
+				$query->select(Translation::getTableName().'.value')
81 81
 					->from(Translation::getTableName())
82
-					->where(Translation::getTableName() . '.translatable_type', '=', \get_class($this))
83
-					->where(Translation::getTableName() . '.locale', '=', $this->locale)
84
-					->where(Translation::getTableName() . '.key', '=', $key)
85
-					->where(Translation::getTableName() . '.translatable_id', '=', \DB::raw($this->getTable() . '.' . $this->primaryKey));
82
+					->where(Translation::getTableName().'.translatable_type', '=', \get_class($this))
83
+					->where(Translation::getTableName().'.locale', '=', $this->locale)
84
+					->where(Translation::getTableName().'.key', '=', $key)
85
+					->where(Translation::getTableName().'.translatable_id', '=', \DB::raw($this->getTable().'.'.$this->primaryKey));
86 86
 			}
87 87
 		]);
88 88
 
@@ -105,10 +105,10 @@  discard block
 block discarded – undo
105 105
 	{
106 106
 		$table = Translation::getTableName();
107 107
 		
108
-		return DB::table($table)->select($table . '.key')
109
-			->where($table . '.translatable_type', \get_class($this))
110
-			->where($table . '.locale', $this->locale)
111
-			->groupBy($table . '.key')
108
+		return DB::table($table)->select($table.'.key')
109
+			->where($table.'.translatable_type', \get_class($this))
110
+			->where($table.'.locale', $this->locale)
111
+			->groupBy($table.'.key')
112 112
 			->pluck('key');
113 113
 	}
114 114
 	
@@ -123,8 +123,8 @@  discard block
 block discarded – undo
123 123
 	
124 124
 	protected function fullTextWildcards($term)
125 125
 	{
126
-		return collect(explode(' ', str_replace(['-', '+', '<', '>', '@', '(', ')', '~'], '', $term)))->map(function ($word, $key) {
127
-			return strlen($word) >= 3 ? '+' . $word . '*' : '';
126
+		return collect(explode(' ', str_replace(['-', '+', '<', '>', '@', '(', ')', '~'], '', $term)))->map(function($word, $key) {
127
+			return strlen($word) >= 3 ? '+'.$word.'*' : '';
128 128
 		})->implode(' ');
129 129
 	}
130 130
 	
@@ -132,10 +132,10 @@  discard block
 block discarded – undo
132 132
 	{
133 133
 		return $query->join('translations', 'translations.translatable_id', '=', 'products.id')
134 134
 			->addSelect([
135
-				'relevance' => function ($query) use ($term) {
136
-					$query->selectRaw("MATCH (`translations`.`value`) AGAINST ('" . $this->fullTextWildcards($term) . "' IN BOOLEAN MODE)")
135
+				'relevance' => function($query) use ($term) {
136
+					$query->selectRaw("MATCH (`translations`.`value`) AGAINST ('".$this->fullTextWildcards($term)."' IN BOOLEAN MODE)")
137 137
 						->from('translations')
138
-						->where('translations.translatable_id', DB::raw($this->getTable() . '.' . $this->primaryKey))
138
+						->where('translations.translatable_id', DB::raw($this->getTable().'.'.$this->primaryKey))
139 139
 						->limit(1);
140 140
 				}
141 141
 			])
Please login to merge, or discard this patch.