1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* rmarchiv.tk |
5
|
|
|
* (c) 2016-2017 by Marcel 'ryg' Hering |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace App\Models; |
9
|
|
|
|
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Spatie\Activitylog\Traits\LogsActivity; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Resource. |
15
|
|
|
* |
16
|
|
|
* @property int $id |
17
|
|
|
* @property string $type |
18
|
|
|
* @property string $cat |
19
|
|
|
* @property int $user_id |
20
|
|
|
* @property string $title |
21
|
|
|
* @property string $desc_md |
22
|
|
|
* @property string $desc_html |
23
|
|
|
* @property string $content_type |
24
|
|
|
* @property string $content_path |
25
|
|
|
* @property \Carbon\Carbon $created_at |
26
|
|
|
* @property \Carbon\Carbon $updated_at |
27
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereId($value) |
28
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereType($value) |
29
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereCat($value) |
30
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereUserId($value) |
31
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereTitle($value) |
32
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereDescMd($value) |
33
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereDescHtml($value) |
34
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereContentType($value) |
35
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereContentPath($value) |
36
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereCreatedAt($value) |
37
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereUpdatedAt($value) |
38
|
|
|
* @mixin \Eloquent |
39
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Venturecraft\Revisionable\Revision[] $revisionHistory |
40
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Activitylog\Models\Activity[] $activity |
41
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Comment[] $comments |
42
|
|
|
* @property-read mixed $votes |
43
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\TagRelation[] $tags |
44
|
|
|
* @property-read \App\Models\User $user |
45
|
|
|
*/ |
46
|
|
|
class Resource extends Model |
47
|
|
|
{ |
48
|
|
|
use \Venturecraft\Revisionable\RevisionableTrait; |
49
|
|
|
use LogsActivity; |
50
|
|
|
|
51
|
|
|
protected $table = 'resources'; |
52
|
|
|
|
53
|
|
|
public $timestamps = true; |
54
|
|
|
|
55
|
|
|
protected $fillable = [ |
56
|
|
|
'type', |
57
|
|
|
'cat', |
58
|
|
|
'user_id', |
59
|
|
|
'title', |
60
|
|
|
'desc_md', |
61
|
|
|
'desc_html', |
62
|
|
|
'content_type', |
63
|
|
|
'content_path', |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
protected $hidden = [ |
67
|
|
|
'votes', |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
protected $guarded = []; |
71
|
|
|
protected $appends = ['votes']; |
72
|
|
|
|
73
|
|
|
public function user(){ |
74
|
|
|
return $this->hasOne('App\Models\User', 'id', 'user_id'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function getVotesAttribute() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$vote['up'] = intval($this->comments()->sum('vote_up')); |
|
|
|
|
80
|
|
|
$vote['down'] = intval($this->comments()->sum('vote_down')); |
81
|
|
|
$vote['avg'] = @round(($vote['up'] - $vote['down']) / ($vote['up'] + $vote['down']), 2); |
|
|
|
|
82
|
|
|
//(voteup - votedown) / (voteup + votedown) |
83
|
|
|
return $vote; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function comments() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
return $this->hasMany('App\Models\Comment', 'content_id', 'id')->Where('content_type', '=', \DB::raw("'resource'"))->with('user'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function tags() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
return $this->hasMany('App\Models\TagRelation', 'content_id', 'id')->Where('content_type', '=', \DB::raw("'resource'"))->with('tag'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.