Completed
Pull Request — master (#34)
by Fèvre
06:03 queued 03:09
created

DiscussThread::slugStrategy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Xetaravel\Models;
3
4
use Eloquence\Behaviours\CountCache\Countable;
5
use Eloquence\Behaviours\Sluggable;
6
use Illuminate\Support\Facades\App;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Route;
9
use Xetaio\Mentions\Models\Traits\HasMentionsTrait;
10
use Xetaravel\Models\Presenters\DiscussThreadPresenter;
11
use Xetaravel\Models\Scopes\DisplayScope;
12
use Xetaravel\Models\User;
13
14
class DiscussThread extends Model
15
{
16
    use Countable,
17
        Sluggable,
18
        DiscussThreadPresenter,
19
        HasMentionsTrait;
20
21
    /**
22
     * The attributes that are mass assignable.
23
     *
24
     * @var array
25
     */
26
    protected $fillable = [
27
        'user_id',
28
        'category_id',
29
        'title',
30
        'slug',
31
        'content',
32
        'content',
33
        'comment_count',
34
        'is_locked',
35
        'is_pinned',
36
        'is_solved',
37
        'is_edited'
38
    ];
39
40
    /**
41
     * The accessors to append to the model's array form.
42
     *
43
     * @var array
44
     */
45
    protected $appends = [
46
        'thread_url'
47
    ];
48
49
    /**
50
     * The attributes that should be mutated to dates.
51
     *
52
     * @var array
53
     */
54
    protected $dates = [
55
        'edited_at'
56
    ];
57
58
    /**
59
     * The "booting" method of the model.
60
     *
61
     * @return void
62
     */
63
    protected static function boot()
64
    {
65
        parent::boot();
66
67
        // Generated the slug before updating.
68
        static::updating(function ($model) {
69
            $model->generateSlug();
70
        });
71
72
        // Set the user id to the new thread before saving it.
73
        static::creating(function ($model) {
74
            $model->user_id = Auth::id();
75
        });
76
    }
77
78
    /**
79
     * Return the field to slug.
80
     *
81
     * @return string
82
     */
83
    public function slugStrategy(): string
84
    {
85
        return 'title';
86
    }
87
88
    /**
89
     * Return the count cache configuration.
90
     *
91
     * @return array
92
     */
93
    public function countCaches(): array
94
    {
95
        return [
96
            User::class,
97
            DiscussCategory::class
98
        ];
99
    }
100
101
    /**
102
     * Get the category that owns the thread.
103
     *
104
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
105
     */
106
    public function category()
107
    {
108
        return $this->belongsTo(DiscussCategory::class);
109
    }
110
111
    /**
112
     * Get the user that owns the thread.
113
     *
114
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
115
     */
116
    public function user()
117
    {
118
        return $this->belongsTo(User::class);
119
    }
120
121
    /**
122
     * Get the comments for the thread.
123
     *
124
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
125
     */
126
    public function comments()
127
    {
128
        return $this->hasMany(DiscussComment::class);
129
    }
130
131
    /**
132
     * Get the solved comment of the thread.
133
     *
134
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
135
     */
136
    public function solvedComment()
137
    {
138
        return $this->hasOne(DiscussComment::class, 'id', 'solved_comment_id');
139
    }
140
141
    /**
142
     * Get the last comment of the thread.
143
     *
144
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
145
     */
146
    public function lastComment()
147
    {
148
        return $this->hasOne(DiscussComment::class, 'id', 'last_comment_id');
149
    }
150
151
    /**
152
     * Get the user that edited the thread.
153
     *
154
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
155
     */
156
    public function editedUser()
157
    {
158
        return $this->hasOne(User::class, 'id', 'edited_user_id');
159
    }
160
}
161