1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fomvasss\Taxonomy\Models\Traits; |
4
|
|
|
|
5
|
|
|
use Fomvasss\Taxonomy\Models\Term; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Трейт для пользовательских классов-моделей котории имею таксономию (статьи, товары,...) |
9
|
|
|
* |
10
|
|
|
* Trait HasTaxonomies |
11
|
|
|
* |
12
|
|
|
* @package App\Models\Traits |
13
|
|
|
*/ |
14
|
|
|
trait HasTaxonomies |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Связь: |
18
|
|
|
* Сущность текущей модели "относится" к разным термам. |
19
|
|
|
* |
20
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
21
|
|
|
*/ |
22
|
|
|
public function terms() |
23
|
|
|
{ |
24
|
|
|
$related = config('taxonomy.models.term', Term::class); |
|
|
|
|
25
|
|
|
|
26
|
|
|
return $this->morphToMany($related, 'termable'); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Связь текущей модели с термамы по указанному system_name словаря. |
31
|
|
|
* Используется для создание связей с термамы нужного словаря в пользовательских моделях. |
32
|
|
|
* |
33
|
|
|
* @param $vocabulary |
34
|
|
|
* @param string|null $vocabularyKey |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
public function termsByVocabulary($vocabulary) |
38
|
|
|
{ |
39
|
|
|
return $this->terms()->where('vocabulary', $vocabulary); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Термы текущей модели по указанному system_name словаря. |
44
|
|
|
* |
45
|
|
|
* @param $query |
46
|
|
|
* @param $vocabulary |
47
|
|
|
* @param string $vocabularyKey |
48
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
49
|
|
|
*/ |
50
|
|
|
public function scopeTermsByVocabulary($query, $vocabulary) |
51
|
|
|
{ |
52
|
|
|
return $query->whereHas('terms', function ($t) use ($vocabulary) { |
53
|
|
|
$t->where('vocabulary', $vocabulary); |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Сущности по указанным термам с соответствующих |
59
|
|
|
* указанных словарей, например: |
60
|
|
|
* получить все Статьи терма-категории "WEB-Программирование" (23) с словаря "Категории статтей" И |
61
|
|
|
* терма-города "Киев" (35) или "Москва" (56) с словаря "Города" |
62
|
|
|
* |
63
|
|
|
* @param $query |
64
|
|
|
* @param array $taxonomies Например: |
65
|
|
|
* [ |
66
|
|
|
* 'article_categories' => 23, |
67
|
|
|
* 'cities' => [35, 56] |
68
|
|
|
* ] |
69
|
|
|
* @param string $termKey |
70
|
|
|
* @param string $vocabularyKey |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function scopeByTaxonomies($query, array $taxonomies, string $termKey = 'id') |
74
|
|
|
{ |
75
|
|
|
foreach ($taxonomies as $vocabulary => $terms) { |
76
|
|
|
$terms = is_array($terms) ? $terms : [$terms]; |
77
|
|
|
if (! empty($terms)) { |
78
|
|
|
$query->whereHas('terms', function ($t) use ($vocabulary, $terms, $termKey) { |
79
|
|
|
$t->where('vocabulary', $vocabulary)->whereIn($termKey, $terms); |
80
|
|
|
}); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $query; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Связь: |
90
|
|
|
* Сущность текущая модель "имеет" один терм. |
91
|
|
|
* Ключ для связи находится в таблице сущности (Ex.: articles.term_id) |
92
|
|
|
* |
93
|
|
|
* @param null $foreignKey |
|
|
|
|
94
|
|
|
* @param null $ownerKey |
|
|
|
|
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function term($foreignKey = null, $ownerKey = null, $relation = null) |
98
|
|
|
{ |
99
|
|
|
$related = config('taxonomy.models.term', Term::class); |
|
|
|
|
100
|
|
|
|
101
|
|
|
return $this->belongsTo($related, $foreignKey, $ownerKey, $relation); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|