1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Models; |
3
|
|
|
|
4
|
|
|
use Eloquence\Behaviours\Sluggable; |
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Xetaravel\Models\Presenters\DiscussCategoryPresenter; |
8
|
|
|
|
9
|
|
|
class DiscussCategory extends Model |
10
|
|
|
{ |
11
|
|
|
use Sluggable, |
12
|
|
|
DiscussCategoryPresenter; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The accessors to append to the model's array form. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $appends = [ |
20
|
|
|
'category_url' |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The "booting" method of the model. |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
protected static function boot() |
29
|
|
|
{ |
30
|
|
|
parent::boot(); |
31
|
|
|
|
32
|
|
|
// Generated the slug before updating. |
33
|
|
|
static::updating(function ($model) { |
34
|
|
|
$model->generateSlug(); |
35
|
|
|
}); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Return the field to slug. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function slugStrategy(): string |
44
|
|
|
{ |
45
|
|
|
return 'title'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the conversations for the category. |
50
|
|
|
* |
51
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
52
|
|
|
*/ |
53
|
|
|
public function conversations() |
54
|
|
|
{ |
55
|
|
|
return $this->hasMany(DiscussConversation::class); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the last conversation of the category. |
60
|
|
|
* |
61
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
62
|
|
|
*/ |
63
|
|
|
public function lastConversation() |
64
|
|
|
{ |
65
|
|
|
return $this->hasOne(DiscussConversation::class, 'id', 'last_conversation_id'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Pluck the categories by the given fields and the locked state. |
70
|
|
|
* |
71
|
|
|
* @param string $value |
72
|
|
|
* @param string|null $column |
73
|
|
|
* |
74
|
|
|
* @return \Illuminate\Support\Collection |
75
|
|
|
*/ |
76
|
|
|
public static function pluckLocked($value, $column = null): Collection |
77
|
|
|
{ |
78
|
|
|
if (Auth::user() && Auth::user()->hasPermission('manage.discuss.conversations')) { |
|
|
|
|
79
|
|
|
return self::pluck($value, $column); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return self::where('is_locked', false)->pluck($value, $column); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: