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\App; |
10
|
|
|
use Illuminate\Support\Facades\Auth; |
11
|
|
|
use Illuminate\Support\Facades\Route; |
12
|
|
|
|
13
|
|
|
class Article extends Model |
14
|
|
|
{ |
15
|
|
|
use Countable, |
16
|
|
|
Sluggable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The "booting" method of the model. |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
protected static function boot() |
24
|
|
|
{ |
25
|
|
|
parent::boot(); |
26
|
|
|
|
27
|
|
|
// The Route::getFacadeRoot() is undefined in the testing environment for mysterious reasons. |
28
|
|
|
if (App::environment() !== 'testing') { |
29
|
|
|
// Don't apply the scope to the admin part. |
30
|
|
|
$result = strpos(Route::getFacadeRoot()->current()->getPrefix(), 'admin'); |
31
|
|
|
|
32
|
|
|
if ($result === false) { |
33
|
|
|
static::addGlobalScope(new DisplayScope); |
34
|
|
|
} |
35
|
|
|
} else { |
36
|
|
|
static::addGlobalScope(new DisplayScope); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Generated the slug before updating. |
40
|
|
|
static::updating(function ($model) { |
41
|
|
|
$model->generateSlug(); |
42
|
|
|
}); |
43
|
|
|
|
44
|
|
|
// Set the user id to the new article before saving it. |
45
|
|
|
static::creating(function ($model) { |
46
|
|
|
$model->user_id = Auth::user()->id; |
|
|
|
|
47
|
|
|
}); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return the field to slug. |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function slugStrategy(): string |
56
|
|
|
{ |
57
|
|
|
return 'title'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Return the count cache configuration. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public function countCaches(): array |
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
|
|
User::class, |
69
|
|
|
Category::class |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the category that owns the article. |
75
|
|
|
* |
76
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
77
|
|
|
*/ |
78
|
|
|
public function category() |
79
|
|
|
{ |
80
|
|
|
return $this->belongsTo(Category::class); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the user that owns the article. |
85
|
|
|
* |
86
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
87
|
|
|
*/ |
88
|
|
|
public function user() |
89
|
|
|
{ |
90
|
|
|
return $this->belongsTo(User::class); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the comments for the article. |
95
|
|
|
* |
96
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
97
|
|
|
*/ |
98
|
|
|
public function comments() |
99
|
|
|
{ |
100
|
|
|
return $this->hasMany(Comment::class); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: