Completed
Pull Request — master (#25)
by Karl
05:00
created

Search::latestNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php namespace JobApis\JobsToMail\Models;
2
3
use Illuminate\Database\Eloquent\Model;
4
use Illuminate\Database\Eloquent\SoftDeletes;
5
use Ramsey\Uuid\Uuid;
6
7
class Search extends Model
8
{
9
    use SoftDeletes;
10
11
    /**
12
     * Indicates that the IDs are not auto-incrementing.
13
     *
14
     * @var bool
15
     */
16
    public $incrementing = false;
17
18
    /**
19
     * The attributes that are mass assignable.
20
     *
21
     * @var array
22
     */
23
    protected $fillable = [
24
        'keyword',
25
        'location',
26
        'no_recruiters',
27
        'user_id',
28
    ];
29
30
    /**
31
     * Boot function from laravel.
32
     */
33 10
    protected static function boot()
34
    {
35 10
        parent::boot();
36
37
        static::creating(function ($model) {
38 2
            $model->{$model->getKeyName()} = Uuid::uuid4();
39 10
        });
40 10
    }
41
42
    /**
43
     * Defines the relationship to User model
44
     *
45
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
46
     */
47 4
    public function user()
48
    {
49 4
        return $this->belongsTo(User::class);
50
    }
51
52
    /**
53
     * Gets the latest Notification only
54
     *
55
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
56
     */
57 1
    public function latestNotification()
58
    {
59 1
        return $this->hasOne(CustomDatabaseNotification::class)
60 1
            ->orderBy('created_at', 'desc');
61
    }
62
63
    /**
64
     * Defines the relationship to Notification model
65
     *
66
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
67
     */
68 1
    public function notifications()
69
    {
70 1
        return $this->hasMany(CustomDatabaseNotification::class);
71
    }
72
73
    /**
74
     * Limits query to "active" searches
75
     *
76
     * @return \Illuminate\Database\Eloquent\Builder
77
     */
78 1
    public function scopeActive($query)
79
    {
80
        return $query->whereHas('user', function ($query) {
81 1
            return $query->confirmed();
82 1
        });
83
    }
84
85
    /**
86
     * Limits query to searches by user with specific email
87
     *
88
     * @return \Illuminate\Database\Eloquent\Builder
89
     */
90 1
    public function scopeWhereUserEmail($query, $email = null)
91
    {
92
        return $query->whereHas('user', function ($query) use ($email) {
93 1
            return $query->where('email', $email);
94 1
        });
95
    }
96
97
    /**
98
     * Limits query to searches by user with specific id
99
     *
100
     * @return \Illuminate\Database\Eloquent\Builder
101
     */
102
    public function scopeWhereUserId($query, $id = null)
103
    {
104 1
        return $query->whereHas('user', function ($query) use ($id) {
105 1
            return $query->where('id', $id);
106 1
        });
107
    }
108
}
109