1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fomvasss\Taxonomy\Models; |
4
|
|
|
|
5
|
|
|
use Fomvasss\Taxonomy\Models\Traits\HasTaxonomies; |
6
|
|
|
use Fomvasss\Taxonomy\Models\Traits\HasTaxonomyablesToMany; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
9
|
|
|
use Kalnoy\Nestedset\NodeTrait; |
10
|
|
|
|
11
|
|
|
class Term extends Model |
12
|
|
|
{ |
13
|
|
|
use HasTaxonomies, |
|
|
|
|
14
|
|
|
HasTaxonomyablesToMany, |
15
|
|
|
NodeTrait; |
16
|
|
|
|
17
|
|
|
protected $guarded = ['id']; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Связь: |
21
|
|
|
* Терм пренадлежит (относится к) одному словарю |
22
|
|
|
* |
23
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
24
|
|
|
*/ |
25
|
|
|
public function txVocabulary() |
26
|
|
|
{ |
27
|
|
|
$related = config('taxonomy.models.vocabulary', Vocabulary::class); |
|
|
|
|
28
|
|
|
|
29
|
|
|
return $this->belongsTo($related, 'vocabulary', 'system_name'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Связь: |
34
|
|
|
* Сущность текущей модели терм "держит" много термов |
35
|
|
|
* |
36
|
|
|
* @return $this |
37
|
|
|
*/ |
38
|
|
|
public function termsByMany() |
39
|
|
|
{ |
40
|
|
|
$related = config('taxonomy.models.term', static::class); |
|
|
|
|
41
|
|
|
|
42
|
|
|
return $this->morphedByMany($related, 'termable'); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Связь: |
47
|
|
|
* Сущность текущей модели "держит" много словарей |
48
|
|
|
* |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function vocabulariesByMany() |
52
|
|
|
{ |
53
|
|
|
$related = config('taxonomy.models.vocabulary', Vocabulary::class); |
|
|
|
|
54
|
|
|
|
55
|
|
|
return $this->morphedByMany($related, 'termable'); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Все термы по указанным словарям |
60
|
|
|
* |
61
|
|
|
* @param $query |
62
|
|
|
* @param $vocabulary |
63
|
|
|
* @param string|null $vocabularyKey |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
public function scopeByVocabulary($query, $vocabulary) |
67
|
|
|
{ |
68
|
|
|
return $query->where('vocabulary', $vocabulary); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|