Skip to content

Commit a74d0d6

Browse files
committed
Post Comments with approvals
1 parent 19e1c88 commit a74d0d6

File tree

8 files changed

+85
-17
lines changed

8 files changed

+85
-17
lines changed

config/blogs.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,12 @@
66
*/
77
'user_class' => \App\User::class,
88
'user_key_name' => 'id',
9+
10+
/*
11+
* The default path to the User model in Laravel
12+
*/
13+
'posts' => [
14+
'allow_comments' => true,
15+
'allow_guest_comments' => true,
16+
],
917
];

database/migrations/2019_12_28_101010_create_posts_table.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ public function up()
2424
$table->string('slug')->nullable();
2525
$table->string('excerpt')->nullable();
2626
$table->text('content')->nullable();
27+
28+
$table->boolean('allow_comments')->default(
29+
config('blogs.posts.allow_comments')
30+
)->nullable();
31+
32+
$table->boolean('allow_guest_comments')->default(
33+
config('blogs.posts.allow_guest_comments')
34+
)->nullable();
35+
2736
$table->string('status')->nullable();
2837
$table->timestamp('published_at')->nullable();
2938
$table->timestamps();

database/migrations/2019_12_28_101012_create_comments_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function up()
2020
$table->text('content');
2121
$table->string('author')->nullable();
2222
$table->string('email')->nullable();
23+
$table->boolean('is_approved')->nullable();
2324
$table->integer('commentable_id');
2425
$table->string('commentable_type');
2526
$table->timestamps();

src/Comment.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class Comment extends Model
2121

2222
public $timestamps = true;
2323

24+
protected $casts = [
25+
'is_approved' => 'boolean',
26+
];
27+
2428
public function commentable()
2529
{
2630
return $this->morphTo();

src/Post.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ public function comments()
4343
return $this->morphMany(Comment::class, 'commentable');
4444
}
4545

46+
public function approvedComments()
47+
{
48+
return $this->comments()->where('is_approved', true);
49+
}
50+
51+
public function disapprovedComments()
52+
{
53+
return $this->comments()->where('is_approved', false);
54+
}
55+
4656
public function tags()
4757
{
4858
return $this->morphToMany(Tag::class, 'taggable');

src/Traits/Comment/CommentApproval.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,6 @@
44

55
trait CommentApproval
66
{
7-
public function scopeApproved($query)
8-
{
9-
return $query->where('is_approved', true);
10-
}
11-
12-
public function scopeUnapproved($query)
13-
{
14-
return $query->where('is_approved', false);
15-
}
16-
177
public function approve()
188
{
199
$this->update(['is_approved' => true]);

src/Traits/Comment/CommentScopes.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@ public function scopeLastMonth(Builder $query)
1616
{
1717
return $query->whereBetween('created_at', [
1818
Carbon::now()->subMonth(), Carbon::now(),
19-
])
20-
->latest();
19+
])->latest();
2120
}
2221

2322
public function scopeLastWeek(Builder $query)
2423
{
2524
return $query->whereBetween('created_at', [
2625
Carbon::now()->subWeek(), Carbon::now(),
27-
])
28-
->latest();
26+
])->latest();
27+
}
28+
29+
public function scopeApproved($query)
30+
{
31+
return $query->where('is_approved', true);
32+
}
33+
34+
public function scopeUnapproved($query)
35+
{
36+
return $query->where('is_approved', false);
2937
}
3038
}

src/Traits/Post/PostAttributes.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Chriscreates\Blog\Traits\Post;
44

5+
use Carbon\Carbon;
6+
57
trait PostAttributes
68
{
79
public function getTagsCountAttribute()
@@ -11,13 +13,49 @@ public function getTagsCountAttribute()
1113

1214
public function isPublished()
1315
{
14-
return $this->status == self::PUBLISHED;
16+
return $this->status == self::PUBLISHED
17+
&& $this->published_at != null;
1518
}
1619

17-
public function isNotPublished()
20+
public function isDraft()
1821
{
1922
return $this->status == self::DRAFT
20-
|| $this->status == self::SCHEDULED;
23+
&& $this->published_at == null;
24+
}
25+
26+
public function isScheduled()
27+
{
28+
return $this->status == self::SCHEDULED
29+
&& $this->published_at > Carbon::now();
30+
}
31+
32+
public function isNotPublished()
33+
{
34+
return $this->isDraft() || $this->isScheduled();
35+
}
36+
37+
public function scheduleFor($date)
38+
{
39+
if ( ! $date instanceof Carbon) {
40+
$date = new Carbon($date);
41+
}
42+
43+
$this->update([
44+
'status' => self::SCHEDULED,
45+
'published_at' => $date,
46+
]);
47+
48+
return $this;
49+
}
50+
51+
public function publish()
52+
{
53+
$this->update([
54+
'status' => self::PUBLISHED,
55+
'published_at' => now(),
56+
]);
57+
58+
return $this;
2159
}
2260

2361
public function getTimeToReadAttribute()

0 commit comments

Comments
 (0)