-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathArticle.php
More file actions
54 lines (44 loc) · 1.13 KB
/
Copy pathArticle.php
File metadata and controls
54 lines (44 loc) · 1.13 KB
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
<?php
namespace App\Models;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Article extends Model
{
use HasFactory;
protected $fillable = [
'slug',
'title',
'excerpt',
'content',
'published_at',
];
protected $casts = [
'published_at' => 'datetime',
];
public function getRouteKeyName()
{
return 'slug';
}
public function scopePublished(Builder $query): void
{
$query
->orderByDesc('published_at')
->whereNotNull('published_at')
->where('published_at', '<=', now());
}
public function author(): BelongsTo
{
return $this->belongsTo(User::class, 'author_id');
}
protected static function boot()
{
parent::boot();
static::creating(function ($article) {
if (auth()->check() && ! $article->author_id) {
$article->author_id = auth()->id();
}
});
}
}