Completed
Pull Request — develop (#38)
by Neil
07:53
created

Notification::scopeRead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Notification extends Model
8
{
9
10
    /**
11
     * The table associated with the model.
12
     *
13
     * @var string
14
     */
15
    protected $table = 'notifications';
16
17
    /**
18
     * The primary key column name.
19
     *
20
     * @var string
21
     */
22
    protected $primaryKey = 'notifications_id';
23
24
    /**
25
     * Indicates if the model should be timestamped.
26
     *
27
     * @var bool
28
     */
29
    public $timestamps = false;
30
31
    public function scopeUnread($query)
32
    {
33
        return $query->leftJoin('notifications_attribs', 'notifications.notifications_id', '=', 'notifications_attribs.notifications_id')->whereNull('user_id')->orWhere(['key'=>'sticky', 'value'=> 1])->limit();
34
    }
35
36
    public function scopeRead($query, $request)
37
    {
38
        $user_id = $request->user()->user_id;
39
        return $query->leftJoin('notifications_attribs', 'notifications.notifications_id', '=', 'notifications_attribs.notifications_id')->where('user_id', $user_id)->where(['key'=>'read', 'value'=> 1])->limit();
40
    }
41
42
    public function scopeLimit($query)
43
    {
44
        return $query->select('notifications.*','key');
45
    }
46
47
    public function users() {
48
        return $this->belongsToMany('App\User')->withPivot('notifications_id','user_id');
49
    }
50
51
}
52