File tree Expand file tree Collapse file tree 4 files changed +60
-2
lines changed Expand file tree Collapse file tree 4 files changed +60
-2
lines changed Original file line number Diff line number Diff line change @@ -19,7 +19,8 @@ public function up()
19
19
$ table ->index ('category_id ' );
20
20
$ table ->integer ('user_id ' )->nullable ();
21
21
$ table ->index ('user_id ' );
22
- $ table ->string ('name ' );
22
+ $ table ->string ('title ' );
23
+ $ table ->string ('sub_title ' );
23
24
$ table ->string ('slug ' )->nullable ();
24
25
$ table ->string ('excerpt ' )->nullable ();
25
26
$ table ->text ('content ' )->nullable ();
Original file line number Diff line number Diff line change 5
5
use Chriscreates \Blog \Traits \IsAuthorable ;
6
6
use Chriscreates \Blog \Traits \Post \PostAttributes ;
7
7
use Chriscreates \Blog \Traits \Post \PostScopes ;
8
+ use Chriscreates \Blog \Traits \Post \PostsHaveACategory ;
8
9
use Chriscreates \Blog \Traits \Post \PostsHaveComments ;
9
10
use Illuminate \Database \Eloquent \Model ;
10
11
@@ -13,7 +14,8 @@ class Post extends Model
13
14
use PostScopes,
14
15
PostAttributes,
15
16
IsAuthorable,
16
- PostsHaveComments;
17
+ PostsHaveComments,
18
+ PostsHaveACategory;
17
19
18
20
const PUBLISHED = 'published ' ;
19
21
const DRAFT = 'draft ' ;
@@ -45,4 +47,13 @@ public function tags()
45
47
{
46
48
return $ this ->morphToMany (Tag::class, 'taggable ' );
47
49
}
50
+
51
+ public static function boot ()
52
+ {
53
+ parent ::boot ();
54
+
55
+ static ::deleted (function (Model $ post ) {
56
+ $ post ->tags ()->detach ();
57
+ });
58
+ }
48
59
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace Chriscreates \Blog \Traits ;
4
4
5
+ use Illuminate \Support \Facades \Auth ;
6
+
5
7
trait IsAuthorable
6
8
{
7
9
public function author ()
@@ -21,4 +23,11 @@ public function user()
21
23
config ('blogs.user_key_name ' )
22
24
);
23
25
}
26
+
27
+ public static function bootBelongsToUser ()
28
+ {
29
+ static ::saving (function ($ model ) {
30
+ $ model ->user_id = $ model ->user_id ?? Auth::id ();
31
+ });
32
+ }
24
33
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments