|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Model; |
|
4
|
|
|
|
|
5
|
|
|
use App\Classes\Repositories\PageRepository; |
|
6
|
|
|
use Carbon\Carbon; |
|
7
|
|
|
use App\Classes\Interfaces\Linkable; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
|
9
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
10
|
|
|
use Laravel\Scout\Searchable; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class Article |
|
14
|
|
|
* |
|
15
|
|
|
* @property int $id |
|
16
|
|
|
* @property string $slug |
|
17
|
|
|
* @property string $title |
|
18
|
|
|
* @property string $content |
|
19
|
|
|
* @property string $featured_img |
|
20
|
|
|
* @property int $category_id |
|
21
|
|
|
* @property int $editor_id |
|
22
|
|
|
* @property int $creator_id |
|
23
|
|
|
* @property boolean $status |
|
24
|
|
|
* |
|
25
|
|
|
* @property ArticleCategory $category |
|
26
|
|
|
* |
|
27
|
|
|
* @property Account $creator |
|
28
|
|
|
* @property Account $editor |
|
29
|
|
|
* |
|
30
|
|
|
* @property Carbon $deleted_at |
|
31
|
|
|
* @property Carbon $created_at |
|
32
|
|
|
* @property Carbon $updated_at |
|
33
|
|
|
* |
|
34
|
|
|
* @package App |
|
35
|
|
|
*/ |
|
36
|
|
|
class Article extends BaseModel implements Linkable |
|
37
|
|
|
{ |
|
38
|
|
|
/* |
|
39
|
|
|
* Laravel Deleting. |
|
40
|
|
|
* |
|
41
|
|
|
* @ https://laravel.com/docs/5.5/eloquent#soft-deleting |
|
42
|
|
|
*/ |
|
43
|
|
|
use SoftDeletes; |
|
44
|
|
|
|
|
45
|
|
|
/* |
|
46
|
|
|
* Laravel Searchable Model. |
|
47
|
|
|
* |
|
48
|
|
|
* @ https://laravel.com/docs/5.3/scout#installation |
|
49
|
|
|
*/ |
|
50
|
|
|
use Searchable; |
|
51
|
|
|
|
|
52
|
|
|
/* |
|
53
|
|
|
* Status conditions column. |
|
54
|
|
|
*/ |
|
55
|
|
|
const STATUS_PUBLISHED = 1; |
|
56
|
|
|
const STATUS_UNPUBLISHED = 0; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* The table associated with the model. |
|
60
|
|
|
* |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $table = 'articles'; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* The attributes that are not mass assignable. |
|
67
|
|
|
* |
|
68
|
|
|
* @var array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $guarded = []; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* The table date columns, casted to Carbon. |
|
74
|
|
|
* |
|
75
|
|
|
* @var array |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Attributes to exclude from the Audit. |
|
81
|
|
|
* |
|
82
|
|
|
* @var array |
|
83
|
|
|
*/ |
|
84
|
|
|
protected $auditExclude = []; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return ArticleCategory|HasOne |
|
88
|
|
|
*/ |
|
89
|
|
|
public function category() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->hasOne(ArticleCategory::class,'id', 'category_id'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function creator() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->belongsTo(Account::class, 'creator_id', 'id'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function editor() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->belongsTo(Account::class, 'editor_id', 'id'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Set the title of the article, and the slug. |
|
106
|
|
|
* |
|
107
|
|
|
* @param $value |
|
108
|
|
|
*/ |
|
109
|
|
|
public function setTitleAttribute($value) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->attributes['title'] = $value; |
|
112
|
|
|
|
|
113
|
|
|
$this->attributes['slug'] = str_slug($value); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* The url that is used to view this model. |
|
118
|
|
|
* |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
public function route() |
|
122
|
|
|
{ |
|
123
|
|
|
/** @var Page $page */ |
|
124
|
|
|
$page = app(PageRepository::class)->wherePlugin('Articles'); |
|
125
|
|
|
|
|
126
|
|
|
if ($this->category) { |
|
127
|
|
|
return "{$page->route()}/{$this->getAttribute('slug')}"; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return "{$page->route()}/{$this->getAttribute('slug')}"; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* The name of the current model object. |
|
135
|
|
|
* |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
|
|
public function name() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->getAttribute('title'); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param int $amount |
|
145
|
|
|
* @return int |
|
146
|
|
|
*/ |
|
147
|
|
|
public function incrementView(int $amount = 1) |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->increment('views', $amount); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|