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
|
|
|
// Don't apply the scope to the admin part. |
28
|
|
|
$result = strpos(Route::getFacadeRoot()->current()->getPrefix(), 'admin'); |
29
|
|
|
|
30
|
|
|
if ($result === false) { |
31
|
|
|
static::addGlobalScope(new DisplayScope); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// Generated the slug before updating. |
35
|
|
|
static::updating(function ($model) { |
36
|
|
|
$model->generateSlug(); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
// Set the user id to the new article before saving it. |
40
|
|
|
static::creating(function ($model) { |
41
|
|
|
$model->user_id = Auth::user()->id; |
|
|
|
|
42
|
|
|
}); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Return the field to slug. |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function slugStrategy(): string |
51
|
|
|
{ |
52
|
|
|
return 'title'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Return the count cache configuration. |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function countCaches(): array |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
User::class, |
64
|
|
|
Category::class |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the category that owns the article. |
70
|
|
|
* |
71
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
72
|
|
|
*/ |
73
|
|
|
public function category() |
74
|
|
|
{ |
75
|
|
|
return $this->belongsTo(Category::class); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the user that owns the article. |
80
|
|
|
* |
81
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
82
|
|
|
*/ |
83
|
|
|
public function user() |
84
|
|
|
{ |
85
|
|
|
return $this->belongsTo(User::class); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the comments for the article. |
90
|
|
|
* |
91
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
92
|
|
|
*/ |
93
|
|
|
public function comments() |
94
|
|
|
{ |
95
|
|
|
return $this->hasMany(Comment::class); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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: