-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArticle.php
146 lines (131 loc) · 3.17 KB
/
Article.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace App\Models;
use Database\Factories\ArticleFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Article extends Model
{
// use soft delete instead of permanent delete
use SoftDeletes;
use HasFactory;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'articles';
protected $fillable = ['title', 'slug', 'description', 'content', 'published', 'published_at'];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at', 'published_at'];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'published' => 'boolean',
];
protected static function newFactory(): ArticleFactory
{
return ArticleFactory::new();
}
/**
* Load all for admin and paginate
*
* @return Paginator
*/
public static function loadAll(): Paginator
{
return static::latest()
->paginate();
}
/**
* Load all for logged in user and paginate
*
* @param $user_id
*
* @return Paginator
*/
public static function loadAllMine(int $user_id): Paginator
{
return static::latest()
->mine($user_id)
->paginate();
}
/**
* load all published with pagination
*
* @return Paginator
*/
public static function loadAllPublished(): Paginator
{
return static::with([
'user' => function (BelongsTo $query) {
$query->select('id', 'name');
},
])
->latest()
->published()
->paginate();
}
/**
* load one published
*
* @param string $slug
*
* @return Article
*/
public static function loadPublished(string $slug): Article
{
return static::with([
'user' => function (BelongsTo $query) {
$query->select('id', 'name');
},
])
->published()
->where('slug', $slug)
->firstOrFail();
}
/**
* Add query scope to get only published articles
*
* @param Builder $query
*
* @return Builder
*/
public function scopePublished(Builder $query): Builder
{
return $query->where([
'published' => true,
]);
}
/**
* Load only articles related with the user id
*
* @param Builder $query
* @param int $user_id
*
* @return Builder
*/
public function scopeMine(Builder $query, int $user_id): Builder
{
return $query->where('user_id', $user_id);
}
/**
* Relationship between articles and user
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}