Skip to content

Commit ba25e97

Browse files
committed
WIP: New fields and scopes for searching posts
1 parent 6ad0a0d commit ba25e97

9 files changed

+180
-78
lines changed

database/migrations/2019_12_28_101010_create_posts_table.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ public function up()
1515
{
1616
Schema::create('posts', function (Blueprint $table) {
1717
$table->increments('id');
18-
$table->integer('category_id');
19-
18+
$table->integer('category_id')->nullable();
19+
$table->index('category_id');
20+
$table->integer('user_id')->nullable();
21+
$table->index('user_id');
2022
$table->string('name');
21-
$table->string('slug');
23+
$table->string('slug')->nullable();
2224
$table->string('excerpt')->nullable();
2325
$table->text('content')->nullable();
2426
$table->string('status')->nullable();
25-
26-
$table->timestamp('published_at');
27+
$table->timestamp('published_at')->nullable();
2728
$table->timestamps();
2829
$table->softDeletes();
2930
});

database/migrations/2019_12_28_101012_create_comments_table.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ public function up()
1515
{
1616
Schema::create('comments', function (Blueprint $table) {
1717
$table->increments('id');
18-
$table->string('content');
18+
$table->integer('user_id')->nullable();
19+
$table->index('user_id');
20+
$table->text('content');
21+
$table->string('author')->nullable();
22+
$table->string('email')->nullable();
1923
$table->integer('commentable_id');
2024
$table->string('commentable_type');
2125
$table->timestamps();

database/migrations/2019_12_28_101013_create_tags_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function up()
1616
Schema::create('tags', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->string('name');
19+
$table->string('slug')->nullable();
1920
$table->timestamps();
2021
});
2122

src/Comment.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Chrisjk123\Blogger;
44

5+
use Chrisjk123\Blogger\Traits\Comment\CommentScopes;
56
use Illuminate\Database\Eloquent\Model;
67

78
class Comment extends Model
89
{
10+
use CommentScopes;
11+
912
protected $table = 'comments';
1013

1114
protected $primaryKey = 'id';
@@ -18,4 +21,9 @@ public function commentable()
1821
{
1922
return $this->morphTo();
2023
}
24+
25+
public function author()
26+
{
27+
return $this->belongsTo(User::class, 'user_id');
28+
}
2129
}

src/Post.php

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

33
namespace Chrisjk123\Blogger;
44

5-
use Chrisjk123\Blogger\Traits\PostAttributes;
6-
use Chrisjk123\Blogger\Traits\PostScopes;
5+
use App\User;
6+
use Chrisjk123\Blogger\Traits\Post\PostAttributes;
7+
use Chrisjk123\Blogger\Traits\Post\PostScopes;
78
use Illuminate\Database\Eloquent\Model;
89

910
class Post extends Model
@@ -24,6 +25,8 @@ class Post extends Model
2425

2526
protected $appends = ['tagsCount'];
2627

28+
protected $dates = ['published_at'];
29+
2730
public function category()
2831
{
2932
return $this->hasOne(Category::class, 'category_id');
@@ -39,8 +42,8 @@ public function tags()
3942
return $this->morphToMany(Tag::class, 'taggable');
4043
}
4144

42-
// public function path()
43-
// {
44-
// return "/posts/{$this->slug}";
45-
// }
45+
public function author()
46+
{
47+
return $this->belongsTo(User::class, 'user_id');
48+
}
4649
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Chrisjk123\Blogger\Traits\Comment;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Database\Eloquent\Builder;
7+
8+
trait CommentScopes
9+
{
10+
public function scopeLatest(Builder $query)
11+
{
12+
return $query->orderBy('created_at', 'DESC');
13+
}
14+
15+
public function scopeLastMonth(Builder $query)
16+
{
17+
return $query->whereBetween('created_at', [
18+
Carbon::now()->subMonth(), Carbon::now(),
19+
])
20+
->latest();
21+
}
22+
23+
public function scopeLastWeek(Builder $query)
24+
{
25+
return $query->whereBetween('created_at', [
26+
Carbon::now()->subWeek(), Carbon::now(),
27+
])
28+
->latest();
29+
}
30+
}

src/Traits/PostAttributes.php renamed to src/Traits/Post/PostAttributes.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Chrisjk123\Blogger\Traits;
3+
namespace Chrisjk123\Blogger\Traits\Post;
44

55
trait PostAttributes
66
{
@@ -9,6 +9,17 @@ public function getTagsCountAttribute()
99
return $this->tags->count();
1010
}
1111

12+
public function isPublished()
13+
{
14+
return $this->status == self::PUBLISHED;
15+
}
16+
17+
public function notPublished()
18+
{
19+
return $this->status == self::DRAFT
20+
|| $this->status == self::SCHEDULED;
21+
}
22+
1223
public function getTimeToReadAttribute()
1324
{
1425
$word_count = str_word_count(strip_tags($this->content));

src/Traits/Post/PostScopes.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace Chrisjk123\Blogger\Traits\Post;
4+
5+
use Carbon\Carbon;
6+
use Chrisjk123\Blogger\Category;
7+
use Chrisjk123\Blogger\Post;
8+
use Illuminate\Database\Eloquent\Builder;
9+
use Illuminate\Support\Collection;
10+
11+
trait PostScopes
12+
{
13+
public function scopeWhereCategories(Builder $query, $categories = null)
14+
{
15+
if (is_null($categories)) {
16+
return $query;
17+
}
18+
19+
if (is_string($categories)) {
20+
return $this->whereCategory($categories, 'name');
21+
}
22+
23+
if (is_int($categories)) {
24+
return $this->whereCategory($categories, 'id');
25+
}
26+
27+
if (is_array($categories)) {
28+
return $query->with('category')
29+
->whereHas('category', function (Builder $query) use ($categories) {
30+
return $query->whereIn('id', $categories);
31+
});
32+
}
33+
34+
if ($categories instanceof Category) {
35+
return $this->whereCategory($categories->id, 'id');
36+
}
37+
38+
if ($categories instanceof Collection) {
39+
return $query->with('category')
40+
->whereHas('category', function (Builder $query) use ($categories) {
41+
return $query->whereIn(
42+
'id',
43+
$categories->pluck('id')->toArray()
44+
);
45+
});
46+
}
47+
48+
return $query;
49+
}
50+
51+
public function scopeWhereCategory(Builder $query, $category, $type = 'name')
52+
{
53+
$query->with('category');
54+
55+
return $query->whereHas('category', function (Builder $query) use ($type, $category) {
56+
return $query->where($type, $category);
57+
});
58+
}
59+
60+
public function scopeRelatedByPostTags(Builder $query, Post $post)
61+
{
62+
return $query->whereHas('tags', function (Builder $query) use ($post) {
63+
return $query->whereIn('name', $post->tags->pluck('name'));
64+
})->where('id', '!=', $post->id);
65+
}
66+
67+
public function scopeRelatedByPostCategory(Builder $query, Post $post)
68+
{
69+
return $query->whereHas('category', function (Builder $query) use ($post) {
70+
return $query->where('id', $post->category->id);
71+
})->where('id', '!=', $post->id);
72+
}
73+
74+
public function scopePublished(Builder $query)
75+
{
76+
return $query->where('status', self::PUBLISHED)
77+
->whereNotNull('published_at');
78+
}
79+
80+
public function scopeNotPublished(Builder $query)
81+
{
82+
return $query->whereIn('status', [
83+
self::DRAFT,
84+
self::SCHEDULED,
85+
])->whereNull('published_at');
86+
}
87+
88+
public function scopeLatest(Builder $query)
89+
{
90+
return $query->orderBy('published_at', 'DESC');
91+
}
92+
93+
public function scopeLastMonth(Builder $query, int $limit = 5)
94+
{
95+
return $query->whereBetween('published_at', [
96+
Carbon::now()->subMonth(), Carbon::now(),
97+
])
98+
->latest()
99+
->limit($limit);
100+
}
101+
102+
public function scopeLastWeek(Builder $query)
103+
{
104+
return $query->whereBetween('published_at', [
105+
Carbon::now()->subWeek(), Carbon::now(),
106+
])
107+
->latest();
108+
}
109+
}

src/Traits/PostScopes.php

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)