1
|
|
|
<?php namespace Arcanesoft\Blog\Models; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Blog\Blog; |
4
|
|
|
use Arcanesoft\Blog\Events\Categories as CategoryEvents; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Category |
8
|
|
|
* |
9
|
|
|
* @package Arcanesoft\Blog\Models |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Category extends AbstractTaxonomy |
13
|
|
|
{ |
14
|
|
|
/* ----------------------------------------------------------------- |
15
|
|
|
| Constants |
16
|
|
|
| ----------------------------------------------------------------- |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
const SELECT_CACHE_NAME = 'blog::categories.select-data'; |
20
|
|
|
|
21
|
|
|
/* ----------------------------------------------------------------- |
22
|
|
|
| Properties |
23
|
|
|
| ----------------------------------------------------------------- |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The database table used by the model |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $table = 'categories'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The event map for the model. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $dispatchesEvents = [ |
39
|
|
|
'creating' => CategoryEvents\CategoryCreating::class, |
40
|
|
|
'created' => CategoryEvents\CategoryCreated::class, |
41
|
|
|
'updating' => CategoryEvents\CategoryUpdating::class, |
42
|
|
|
'updated' => CategoryEvents\CategoryUpdated::class, |
43
|
|
|
'saving' => CategoryEvents\CategorySaving::class, |
44
|
|
|
'saved' => CategoryEvents\CategorySaved::class, |
45
|
|
|
'deleting' => CategoryEvents\CategoryDeleting::class, |
46
|
|
|
'deleted' => CategoryEvents\CategoryDeleted::class, |
47
|
|
|
'restoring' => CategoryEvents\CategoryRestoring::class, |
48
|
|
|
'restored' => CategoryEvents\CategoryRestored::class, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/* ----------------------------------------------------------------- |
52
|
|
|
| Relationships |
53
|
|
|
| ----------------------------------------------------------------- |
54
|
|
|
*/ |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Relationship with posts. |
58
|
|
|
* |
59
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
60
|
|
|
*/ |
61
|
2 |
|
public function posts() |
62
|
|
|
{ |
63
|
2 |
|
return $this->hasMany(Post::class); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/* ----------------------------------------------------------------- |
67
|
|
|
| Main Methods |
68
|
|
|
| ----------------------------------------------------------------- |
69
|
|
|
*/ |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the categories options for select input. |
73
|
|
|
* |
74
|
|
|
* @param bool $placeholder |
75
|
|
|
* |
76
|
|
|
* @return \Illuminate\Support\Collection |
77
|
|
|
*/ |
78
|
2 |
|
public static function getSelectData($placeholder = true) |
79
|
|
|
{ |
80
|
2 |
|
$minutes = config('arcanesoft.blog.cache.categories.select-data', 5); |
81
|
|
|
|
82
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection $categories */ |
83
|
2 |
|
return cache()->remember(self::SELECT_CACHE_NAME, $minutes, function () { |
84
|
2 |
|
$withTranslations = Blog::instance()->isTranslatable(); |
85
|
|
|
|
86
|
2 |
|
return self::all()->mapWithKeys(function (Category $category) use ($withTranslations) { |
87
|
|
|
return [ |
88
|
2 |
|
$category->id => $withTranslations |
89
|
|
|
? implode(' / ', $category->getTranslations('name')) |
90
|
2 |
|
: $category->name |
91
|
|
|
]; |
92
|
2 |
|
}); |
93
|
2 |
|
}) |
94
|
2 |
|
->toBase()->when($placeholder, function ($categories) { |
95
|
|
|
/** @var \Illuminate\Support\Collection $categories */ |
96
|
2 |
|
return $categories->prepend(trans('blog::categories.select-category'), 0); |
97
|
2 |
|
}); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|