diff --git a/app/Http/Controllers/RepliesController.php b/app/Http/Controllers/RepliesController.php new file mode 100644 index 0000000..70f0321 --- /dev/null +++ b/app/Http/Controllers/RepliesController.php @@ -0,0 +1,62 @@ +middleware('auth', ['except' => ['index', 'show']]); + } + + public function index() + { + $replies = Reply::paginate(); + return view('replies.index', compact('replies')); + } + + public function show(Reply $reply) + { + return view('replies.show', compact('reply')); + } + + public function create(Reply $reply) + { + return view('replies.create_and_edit', compact('reply')); + } + + public function store(ReplyRequest $request) + { + $reply = new Reply(); + $reply->setAttribute('topic_id', $request->get('topic_id')); + $reply->setAttribute('user_id', \Auth::id()); + $reply->setAttribute('content', $request->get('content')); + $reply->save(); + return redirect()->back()->with('success', '回复成功.'); + } + + public function edit(Reply $reply) + { + $this->authorize('update', $reply); + return view('replies.create_and_edit', compact('reply')); + } + + public function update(ReplyRequest $request, Reply $reply) + { + $this->authorize('update', $reply); + $reply->update($request->all()); + + return redirect()->route('replies.show', $reply->id)->with('message', 'Updated successfully.'); + } + + public function destroy(Reply $reply) + { + $this->authorize('destroy', $reply); + $reply->delete(); + + return redirect()->back()->with('success', '删除成功.'); + } +} \ No newline at end of file diff --git a/app/Http/Requests/ReplyRequest.php b/app/Http/Requests/ReplyRequest.php new file mode 100644 index 0000000..cd62bed --- /dev/null +++ b/app/Http/Requests/ReplyRequest.php @@ -0,0 +1,50 @@ +method()) { + // CREATE + case 'POST': + { + return [ + 'content' => 'required|max:255', + 'topic_id' => 'required|integer' + ]; + } + // UPDATE + case 'PUT': + case 'PATCH': + { + return [ + // UPDATE ROLES + ]; + } + case 'GET': + case 'DELETE': + default: + { + return []; + }; + } + } + + public function messages() + { + return [ + // Validation messages + ]; + } + + + public function attributes() + { + return [ + 'content' => '回复内容', + 'topic_id' => '回复的主题' + ]; + } +} diff --git a/app/Observers/ReplyObserver.php b/app/Observers/ReplyObserver.php index c646a8e..aee8d0b 100644 --- a/app/Observers/ReplyObserver.php +++ b/app/Observers/ReplyObserver.php @@ -11,11 +11,16 @@ class ReplyObserver { public function creating(Reply $reply) { - // + $reply->setAttribute('content', clean($reply->getAttribute('content'), 'user_topic_body')); } public function updating(Reply $reply) { // } + + public function created(Reply $reply) + { + $reply->topic->increment('reply_count', 1); + } } \ No newline at end of file diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 3d89c32..3520af8 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,7 +2,9 @@ namespace App\Providers; +use App\Models\Reply; use App\Models\Topic; +use App\Observers\ReplyObserver; use App\Observers\TopicObserver; use Carbon\Carbon; use Illuminate\Support\ServiceProvider; @@ -18,6 +20,7 @@ class AppServiceProvider extends ServiceProvider { Carbon::setLocale('zh'); Topic::observe(new TopicObserver()); + Reply::observe(new ReplyObserver()); } diff --git a/resources/views/topics/_reply_box.blade.php b/resources/views/topics/_reply_box.blade.php index e69de29..1aec88d 100644 --- a/resources/views/topics/_reply_box.blade.php +++ b/resources/views/topics/_reply_box.blade.php @@ -0,0 +1,12 @@ +@include('common.error') +
+
+ + +
+ +
+ +
+
+
\ No newline at end of file diff --git a/resources/views/topics/show.blade.php b/resources/views/topics/show.blade.php index 2714a57..c3b38e1 100644 --- a/resources/views/topics/show.blade.php +++ b/resources/views/topics/show.blade.php @@ -67,7 +67,7 @@ {{-- 用户回复列表 --}}
- @include('topics._reply_box', ['topic' => $topic]) + @includeWhen(Auth::check(),'topics._reply_box', ['topic' => $topic]) @include('topics._reply_list', ['replies' => $topic->replies()->with('user')->get()])
diff --git a/routes/web.php b/routes/web.php index f43c968..42b9b53 100644 --- a/routes/web.php +++ b/routes/web.php @@ -38,4 +38,4 @@ Route::resource('categories', 'CategoriesController', ['only' => ['show']]); Route::post('upload_image', 'TopicsController@uploadImage')->name('topics.upload_image'); -Route::resource('replies', 'RepliesController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]); \ No newline at end of file +Route::resource('replies', 'RepliesController', ['only' => ['store', 'destroy']]); \ No newline at end of file