1
|
|
|
<?php namespace Cviebrock\EloquentTaggable\Models; |
2
|
|
|
|
3
|
|
|
use Cviebrock\EloquentTaggable\Services\TagService; |
4
|
|
|
use Illuminate\Database\Eloquent\Builder; |
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Tag |
12
|
|
|
*/ |
13
|
|
|
class Tag extends Model |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @inheritdoc |
18
|
|
|
*/ |
19
|
|
|
protected $table; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @inheritdoc |
23
|
|
|
*/ |
24
|
|
|
protected $primaryKey = 'tag_id'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
|
|
protected $fillable = [ |
30
|
|
|
'name', |
31
|
|
|
'normalized', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public function __construct(array $attributes = []) |
38
|
|
|
{ |
39
|
|
|
if ($connection = config('taggable.connection')) { |
40
|
|
|
$this->setConnection($connection); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$table = config('taggable.tables.taggable_tags', 'taggable_tags'); |
44
|
23 |
|
$this->setTable($table); |
45
|
|
|
|
46
|
23 |
|
parent::__construct($attributes); |
47
|
23 |
|
} |
48
|
23 |
|
|
49
|
23 |
|
/** |
50
|
|
|
* Set the name attribute on the model. |
51
|
|
|
* |
52
|
|
|
* @param string $value |
53
|
|
|
*/ |
54
|
|
|
public function setNameAttribute($value) |
55
|
|
|
{ |
56
|
|
|
$value = trim($value); |
57
|
|
|
$this->attributes['name'] = $value; |
58
|
|
|
$this->attributes['normalized'] = app(TagService::class)->normalize($value); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Scope to find tags by name. |
63
|
|
|
* |
64
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
65
|
|
|
* @param $value |
66
|
|
|
* |
67
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
68
|
|
|
*/ |
69
|
|
|
public function scopeByName(Builder $query, string $value): Builder |
70
|
|
|
{ |
71
|
|
|
$normalized = app(TagService::class)->normalize($value); |
72
|
|
|
|
73
|
|
|
return $query->where('normalized', $normalized); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function getRelationValue($key) |
80
|
|
|
{ |
81
|
|
|
// Check for regular relation first |
82
|
|
|
if ($return = parent::getRelationValue($key)) { |
83
|
|
|
return $return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Check if the relation is defined via configuration |
87
|
|
|
$relatedClass = Arr::get(config('taggable.taggedModels'), $key); |
88
|
|
|
|
89
|
|
|
if ($relatedClass) { |
90
|
|
|
$relation = $this->taggedModels($relatedClass); |
91
|
|
|
|
92
|
|
|
return tap($relation->getResults(), function($results) use ($key) { |
93
|
|
|
$this->setRelation($key, $results); |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the inverse of the polymorphic relation, via an attribute |
100
|
|
|
* defining the type of models to return. |
101
|
|
|
* |
102
|
|
|
* @param string $class |
103
|
|
|
* |
104
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
105
|
|
|
*/ |
106
|
|
|
protected function taggedModels(string $class): MorphToMany |
107
|
|
|
{ |
108
|
|
|
$table = config('taggable.tables.taggable_taggables', 'taggable_taggables'); |
109
|
|
|
|
110
|
|
|
return $this->morphedByMany($class, 'taggable', $table, 'tag_id'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Find the tag with the given name. |
115
|
|
|
* |
116
|
|
|
* @param string $value |
117
|
|
|
* |
118
|
|
|
* @return static|null |
119
|
|
|
*/ |
120
|
|
|
public static function findByName(string $value) |
121
|
|
|
{ |
122
|
|
|
return app(TagService::class)->find($value); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @inheritdoc |
127
|
|
|
*/ |
128
|
|
|
public function __toString(): string |
129
|
|
|
{ |
130
|
|
|
return (string) $this->getAttribute('name'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|