generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPost.php
132 lines (115 loc) · 3.28 KB
/
Post.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
<?php
namespace App\Models;
use Orbit\Concerns\Orbital;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use BeyondCode\Comments\Traits\HasComments;
class Post extends Model
{
use Orbital;
use HasComments;
public static function schema(Blueprint $table)
{
$table->id();
$table->string('title');
$table->string('tldr')->nullable();
$table->string('slug')->unique();
$table->text('content')->nullable();
$table->boolean('featured')->default(false);
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('category_id');
$table->string('main_image')->nullable();
$table->string('status')->default('draft');
$table->dateTime('published_at')->nullable();
$table->dateTime('unpublished_at')->nullable();
}
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'title',
'tldr',
'slug',
'content',
'featured',
'user_id',
'category_id',
'main_image',
'status',
'published_at',
'unpublished_at'
];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
];
/**
* Find user associated with this post
*
* @return User
*/
public function user()
{
return $this->belongsTo(User::class);
}
/**
* Find category associated with this post
*
* @return Category
*
*/
public function category()
{
return $this->belongsTo(Category::class);
}
// Get posts from this user
public function tags()
{
return $this->belongsToMany(Tag::class)->withPivot('id')->using(PostTag::class);
}
// Get the posts have to be published
public function scopeToBePublished($query)
{
return $query->where('status', 'scheduledtopublish')->where('published_at', '<=', now())->get();
}
// Get the posts have to be published
public function scopeToBeUnpublished($query)
{
return $query->where('status', 'scheduledtounpublish')->where('unpublished_at', '<=', now())->get();
}
// Procceed publishing for every posts catched by scopeToBePublished function
public static function updatePostToBePublished()
{
foreach (Post::toBePublished() as $post) {
$post->update([
'status' => 'published',
'published_at' => now()
]);
Log::debug('The post with id : #' . $post->id . ' was updated with status PUBLISHED at ' . now());
}
}
// Procceed publishing for every posts catched by scopeToBePublished function
public static function updatePostToBeUnpublished()
{
foreach (Post::toBeUnpublished() as $post) {
$post->update([
'status' => 'unpublished',
'unpublished_at' => now()
]);
Log::debug('The post with id : #' . $post->id . ' was updated with status UNPUBLISHED at ' . now());
}
}
}