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

DiscussThread::user()   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
        'last_page'
48
    ];
49
50
    /**
51
     * The attributes that should be mutated to dates.
52
     *
53
     * @var array
54
     */
55
    protected $dates = [
56
        'edited_at'
57
    ];
58
59
    /**
60
     * The "booting" method of the model.
61
     *
62
     * @return void
63
     */
64
    protected static function boot()
65
    {
66
        parent::boot();
67
68
        // Generated the slug before updating.
69
        static::updating(function ($model) {
70
            $model->generateSlug();
71
        });
72
73
        // Set the user id to the new thread before saving it.
74
        static::creating(function ($model) {
75
            $model->user_id = Auth::id();
76
        });
77
    }
78
79
    /**
80
     * Return the field to slug.
81
     *
82
     * @return string
83
     */
84
    public function slugStrategy(): string
85
    {
86
        return 'title';
87
    }
88
89
    /**
90
     * Return the count cache configuration.
91
     *
92
     * @return array
93
     */
94
    public function countCaches(): array
95
    {
96
        return [
97
            User::class,
98
            DiscussCategory::class
99
        ];
100
    }
101
102
    /**
103
     * Get the category that owns the thread.
104
     *
105
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
106
     */
107
    public function category()
108
    {
109
        return $this->belongsTo(DiscussCategory::class);
110
    }
111
112
    /**
113
     * Get the user that owns the thread.
114
     *
115
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
116
     */
117
    public function user()
118
    {
119
        return $this->belongsTo(User::class);
120
    }
121
122
    /**
123
     * Get the comments for the thread.
124
     *
125
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
126
     */
127
    public function comments()
128
    {
129
        return $this->hasMany(DiscussComment::class);
130
    }
131
132
    /**
133
     * Get the solved comment of the thread.
134
     *
135
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
136
     */
137
    public function solvedComment()
138
    {
139
        return $this->hasOne(DiscussComment::class, 'id', 'solved_comment_id');
140
    }
141
142
    /**
143
     * Get the last comment of the thread.
144
     *
145
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
146
     */
147
    public function lastComment()
148
    {
149
        return $this->hasOne(DiscussComment::class, 'id', 'last_comment_id');
150
    }
151
152
    /**
153
     * Get the user that edited the thread.
154
     *
155
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
156
     */
157
    public function editedUser()
158
    {
159
        return $this->hasOne(User::class, 'id', 'edited_user_id');
160
    }
161
}
162