Completed
Branch master (a6481d)
by Fèvre
02:09
created

Article::category()   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
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 Illuminate\Support\Facades\Route;
9
use Illuminate\Support\Facades\App;
10
11
class Article extends Model
12
{
13
    use Countable;
14
15
    /**
16
     * The "booting" method of the model.
17
     *
18
     * @return void
19
     */
20
    protected static function boot()
21
    {
22
        parent::boot();
23
        
24
        /**
25
         * The Route::getFacadeRoot() is undefined in the testing environment for mysterious reasons.
26
         */
27
        if (App::environment() != 'testing') {
28
            // Don't apply the scope to the admin part.
29
            if (Route::getFacadeRoot()->current()->getPrefix() != '/admin') {
30
                static::addGlobalScope(new DisplayScope);
31
            }
32
        } else {
33
            static::addGlobalScope(new DisplayScope);
34
        }
35
    }
36
37
    /**
38
     * Return the count cache configuration.
39
     *
40
     * @return array
41
     */
42
    public function countCaches(): array
43
    {
44
        return [
45
            User::class,
46
            Category::class
47
        ];
48
    }
49
50
    /**
51
     * Get the category that owns the article.
52
     *
53
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
54
     */
55
    public function category()
56
    {
57
        return $this->belongsTo('Xetaravel\Models\Category');
58
    }
59
60
    /**
61
     * Get the user that owns the article.
62
     *
63
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
64
     */
65
    public function user()
66
    {
67
        return $this->belongsTo('Xetaravel\Models\User');
68
    }
69
70
    /**
71
     * Get the comments for the article.
72
     *
73
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
74
     */
75
    public function comments()
76
    {
77
        return $this->hasMany('Xetaravel\Models\Comment');
78
    }
79
}
80