1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Models; |
3
|
|
|
|
4
|
|
|
use Xetaravel\Models\Category; |
5
|
|
|
use Xetaravel\Models\User; |
6
|
|
|
use Xetaravel\Models\Scopes\DisplayScope; |
7
|
|
|
use Eloquence\Behaviours\CountCache\Countable; |
8
|
|
|
use Eloquence\Behaviours\Sluggable; |
9
|
|
|
use Illuminate\Support\Facades\Route; |
10
|
|
|
use Illuminate\Support\Facades\App; |
11
|
|
|
|
12
|
|
|
class Article extends Model |
13
|
|
|
{ |
14
|
|
|
use Countable, |
15
|
|
|
Sluggable; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The "booting" method of the model. |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
protected static function boot() |
23
|
|
|
{ |
24
|
|
|
parent::boot(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The Route::getFacadeRoot() is undefined in the testing environment for mysterious reasons. |
28
|
|
|
*/ |
29
|
|
|
if (App::environment() != 'testing') { |
30
|
|
|
// Don't apply the scope to the admin part. |
31
|
|
|
if (Route::getFacadeRoot()->current()->getPrefix() != '/admin') { |
32
|
|
|
static::addGlobalScope(new DisplayScope); |
33
|
|
|
} |
34
|
|
|
} else { |
35
|
|
|
static::addGlobalScope(new DisplayScope); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Return the field to slug. |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function slugStrategy(): string |
45
|
|
|
{ |
46
|
|
|
return 'title'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return the count cache configuration. |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function countCaches(): array |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
User::class, |
58
|
|
|
Category::class |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the category that owns the article. |
64
|
|
|
* |
65
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
66
|
|
|
*/ |
67
|
|
|
public function category() |
68
|
|
|
{ |
69
|
|
|
return $this->belongsTo(Category::class); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the user that owns the article. |
74
|
|
|
* |
75
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
76
|
|
|
*/ |
77
|
|
|
public function user() |
78
|
|
|
{ |
79
|
|
|
return $this->belongsTo(User::class); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the comments for the article. |
84
|
|
|
* |
85
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
86
|
|
|
*/ |
87
|
|
|
public function comments() |
88
|
|
|
{ |
89
|
|
|
return $this->hasMany(Comment::class); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|