File tree Expand file tree Collapse file tree 4 files changed +95
-0
lines changed
resources/views/blog/posts Expand file tree Collapse file tree 4 files changed +95
-0
lines changed Original file line number Diff line number Diff line change @@ -17,4 +17,38 @@ public function index()
17
17
//return blog.blade.php template from resources/views folder
18
18
return view ('blog.index ' , ['posts ' => $ posts , 'title ' => 'Blog Posts ' ]);
19
19
}
20
+
21
+ public function create (Request $ request )
22
+ {
23
+ if ($ request ->user ()->canManagePosts ()) {
24
+ return view ('blog.posts.create ' );
25
+ } else {
26
+ return redirect ('/ ' )
27
+ ->withErrors ('You do not have permission to create posts. ' );
28
+ }
29
+ }
30
+
31
+ public function save (Request $ request )
32
+ {
33
+ $ post = new Post ();
34
+ $ post ->title = $ request ->get ('title ' );
35
+ $ post ->metaTitle = $ request ->get ('metaTitle ' );
36
+ $ post ->body = $ request ->get ('body ' );
37
+ $ post ->metaDescription = $ request ->get ('metaDescription ' );
38
+ $ post ->slug = $ request ->get ('urlSlug ' ) ?? Str::slug ($ post ->title );
39
+ $ post ->user_id = $ request ->user ()->id ;
40
+
41
+ if ($ request ->has ('draft ' )) {
42
+ $ post ->active = 0 ;
43
+ $ message = 'Draft saved! ' ;
44
+ } else {
45
+ $ post ->active = 1 ;
46
+ $ message = 'Post published! ' ;
47
+ }
48
+
49
+ $ post ->save ();
50
+
51
+ return redirect ('admin/blog/post/ ' . $ post ->slug )
52
+ ->withMessage ($ message );
53
+ }
20
54
}
Original file line number Diff line number Diff line change @@ -51,4 +51,22 @@ public function comments()
51
51
return $ this ->hasMany ('App\Models\Comments ' , 'user_id ' );
52
52
}
53
53
54
+ /**
55
+ * Check if a user can CRUD posts
56
+ */
57
+ public function canManagePosts (): bool
58
+ {
59
+ if ($ this ->role === 'author ' || $ this ->isAdmin ()) {
60
+ return true ;
61
+ }
62
+ return false ;
63
+ }
64
+
65
+ /**
66
+ * Check if the user is an admin
67
+ */
68
+ public function isAdmin (): bool
69
+ {
70
+ return $ this ->role === 'admin ' ;
71
+ }
54
72
}
Original file line number Diff line number Diff line change
1
+ @extends (' layouts.app' )
2
+
3
+ @section (' content' )
4
+ @if (session (' message' ) )
5
+ <div class =" alert alert-success" >
6
+ {{ session (' message' ) } }
7
+ </div >
8
+ @endif
9
+
10
+ <form action =" /admin/blog/post" method =" post" class =" container" >
11
+ <input type =" hidden" name =" _token" value =" {{ csrf_token () } }" >
12
+ <div class =" form-group" >
13
+ <label for =" title" >Title</label >
14
+ <input type =" text" name =" title" class =" form-control" />
15
+ </div >
16
+ <div class =" form-group" >
17
+ <label for =" metaTitle" >Meta Title</label >
18
+ <input type =" text" name =" metaTitle" class =" form-control" />
19
+ </div >
20
+ <div class =" form-group" >
21
+ <label for =" body" >Body</label >
22
+ <textarea name =" body" class =" form-control" ></textarea >
23
+ </div >
24
+ <div class =" form-group" >
25
+ <label for =" metaDescription" >Meta Description</label >
26
+ <textarea name =" metaDescription" class =" form-control" ></textarea >
27
+ </div >
28
+ <div class =" form-group" >
29
+ <label for =" urlSlug" >URL Slug (optional)</label >
30
+ <small >If not specified one will be created from the title</small >
31
+ <input type =" text" name =" urlSlug" class =" form-control" />
32
+ </div >
33
+ <input type =" submit" name =" publish" class =" btn btn-success" value =" Publish" />
34
+ <input type =" submit" name =" draft" class =" btn btn-default" value =" Save Draft" />
35
+ </form >
36
+ @stop
Original file line number Diff line number Diff line change 18
18
Route::get ('/blog ' , 'App\Http\Controllers\PostController@index ' );
19
19
20
20
Auth::routes ();
21
+
22
+ Route::group (['prefix ' => 'admin ' ], function () {
23
+ Route::group (['prefix ' => 'blog ' ], function () {
24
+ Route::get ('/post ' , 'App\Http\Controllers\PostController@create ' );
25
+ Route::post ('/post ' , 'App\Http\Controllers\PostController@save ' );
26
+ });
27
+ });
You can’t perform that action at this time.
0 commit comments