Skip to content

Commit 36053ec

Browse files
committed
Additional fields for posts and methods for post filtering by categories
1 parent 8f843b1 commit 36053ec

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

database/migrations/2019_12_28_101010_create_posts_table.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public function up()
1919
$table->index('category_id');
2020
$table->integer('user_id')->nullable();
2121
$table->index('user_id');
22-
$table->string('name');
22+
$table->string('title');
23+
$table->string('sub_title');
2324
$table->string('slug')->nullable();
2425
$table->string('excerpt')->nullable();
2526
$table->text('content')->nullable();

src/Post.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Chriscreates\Blog\Traits\IsAuthorable;
66
use Chriscreates\Blog\Traits\Post\PostAttributes;
77
use Chriscreates\Blog\Traits\Post\PostScopes;
8+
use Chriscreates\Blog\Traits\Post\PostsHaveACategory;
89
use Chriscreates\Blog\Traits\Post\PostsHaveComments;
910
use Illuminate\Database\Eloquent\Model;
1011

@@ -13,7 +14,8 @@ class Post extends Model
1314
use PostScopes,
1415
PostAttributes,
1516
IsAuthorable,
16-
PostsHaveComments;
17+
PostsHaveComments,
18+
PostsHaveACategory;
1719

1820
const PUBLISHED = 'published';
1921
const DRAFT = 'draft';
@@ -45,4 +47,13 @@ public function tags()
4547
{
4648
return $this->morphToMany(Tag::class, 'taggable');
4749
}
50+
51+
public static function boot()
52+
{
53+
parent::boot();
54+
55+
static::deleted(function (Model $post) {
56+
$post->tags()->detach();
57+
});
58+
}
4859
}

src/Traits/IsAuthorable.php

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

33
namespace Chriscreates\Blog\Traits;
44

5+
use Illuminate\Support\Facades\Auth;
6+
57
trait IsAuthorable
68
{
79
public function author()
@@ -21,4 +23,11 @@ public function user()
2123
config('blogs.user_key_name')
2224
);
2325
}
26+
27+
public static function bootBelongsToUser()
28+
{
29+
static::saving(function ($model) {
30+
$model->user_id = $model->user_id ?? Auth::id();
31+
});
32+
}
2433
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Chriscreates\Blog\Traits\Post;
4+
5+
use Chriscreates\Blog\Category;
6+
use Illuminate\Support\Collection;
7+
8+
trait PostsHaveACategory
9+
{
10+
public function hasCategory($category)
11+
{
12+
if ($category instanceof Category) {
13+
$category = $category->id;
14+
}
15+
16+
if (is_null($this->category)) {
17+
return false;
18+
}
19+
20+
return $category_id == $this->category->id;
21+
}
22+
23+
public function hasAnyCategory($categories)
24+
{
25+
if ($categories instanceof Collection) {
26+
$categories = $categories->pluck('id')->toArray();
27+
}
28+
29+
foreach ($categories as $category) {
30+
if ($this->hasCategory($category)) {
31+
return true;
32+
}
33+
}
34+
35+
return false;
36+
}
37+
}

0 commit comments

Comments
 (0)