1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Spatie\Blender\Model\Model; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use App\Models\Enums\SpecialArticle; |
9
|
|
|
use Spatie\EloquentSortable\Sortable; |
10
|
|
|
use Spatie\Blender\Model\Traits\HasSlug; |
11
|
|
|
use Spatie\EloquentSortable\SortableTrait; |
12
|
|
|
use App\Models\Presenters\ArticlePresenter; |
13
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
14
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @property \App\Models\Article $parent |
18
|
|
|
* @property \Illuminate\Support\Collection $children |
19
|
|
|
* @property \App\Models\Article $firstChild |
20
|
|
|
* @property \Illuminate\Support\Collection $siblings |
21
|
|
|
*/ |
22
|
|
|
class Article extends Model implements Sortable |
23
|
|
|
{ |
24
|
|
|
use ArticlePresenter, HasSlug, SortableTrait; |
25
|
|
|
|
26
|
|
|
protected $with = ['media']; |
27
|
|
|
|
28
|
|
|
protected $mediaLibraryCollections = ['images', 'downloads']; |
29
|
|
|
|
30
|
|
|
public $translatable = ['name', 'text', 'slug', 'seo_values']; |
31
|
|
|
|
32
|
|
|
public function registerMediaConversions() |
33
|
|
|
{ |
34
|
|
|
parent::registerMediaConversions(); |
35
|
|
|
|
36
|
|
|
$this->addMediaConversion('thumb') |
37
|
|
|
->setWidth(368) |
38
|
|
|
->setHeight(232) |
39
|
|
|
->performOnCollections('images'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function isSpecialArticle($specialArticleName = ''): bool |
43
|
|
|
{ |
44
|
|
|
if ($specialArticleName === '') { |
45
|
|
|
return ! empty($this->technical_name); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this->technical_name === $specialArticleName; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function children(): HasMany |
52
|
|
|
{ |
53
|
|
|
return $this->hasMany(self::class, 'parent_id')->orderBy('order_column'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function hasChildren(): bool |
57
|
|
|
{ |
58
|
|
|
return count($this->children); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getFirstChildAttribute(): Article |
62
|
|
|
{ |
63
|
|
|
if (! $this->hasChildren()) { |
64
|
|
|
throw new Exception("Article `{$this->id}` doesn't have any children."); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->children->sortBy('order_column')->first(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getSiblingsAttribute(): Collection |
71
|
|
|
{ |
72
|
|
|
return self::where('parent_id', $this->parent_id) |
|
|
|
|
73
|
|
|
->orderBy('order_column') |
74
|
|
|
->get(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function parent(): BelongsTo |
78
|
|
|
{ |
79
|
|
|
return $this->belongsTo(self::class, 'parent_id'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function hasParent(): bool |
83
|
|
|
{ |
84
|
|
|
return ! is_null($this->parent); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getUrlAttribute(): string |
88
|
|
|
{ |
89
|
|
|
$localeSegment = ''; |
90
|
|
|
|
91
|
|
|
if (locales()->count() > 1) { |
92
|
|
|
$localeSegment = '/'.locale(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($this->technical_name === SpecialArticle::HOME) { |
|
|
|
|
96
|
|
|
return $localeSegment; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$parentSlug = $this->hasParent() ? $this->parent->slug.'/' : ''; |
|
|
|
|
100
|
|
|
|
101
|
|
|
return "{$localeSegment}/{$parentSlug}{$this->slug}"; |
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.