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