发布回复

This commit is contained in:
fthvgb1 2018-01-15 23:59:30 +08:00
parent 46304b7935
commit ad94cc05dd
7 changed files with 135 additions and 3 deletions

View File

@ -0,0 +1,62 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\ReplyRequest;
use App\Models\Reply;
class RepliesController extends Controller
{
public function __construct()
{
$this->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', '删除成功.');
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Http\Requests;
class ReplyRequest extends Request
{
public function rules()
{
switch ($this->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' => '回复的主题'
];
}
}

View File

@ -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);
}
}

View File

@ -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());
}

View File

@ -0,0 +1,12 @@
@include('common.error')
<div class="reply-box">
<form action="{{ route('replies.store') }}" method="POST" accept-charset="UTF-8">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="topic_id" value="{{ $topic->id }}">
<div class="form-group">
<textarea class="form-control" rows="3" placeholder="分享你的想法" name="content"></textarea>
</div>
<button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-share"></i>回复</button>
</form>
</div>
<hr>

View File

@ -67,7 +67,7 @@
{{-- 用户回复列表 --}}
<div class="panel panel-default topic-reply">
<div class="panel-body">
@include('topics._reply_box', ['topic' => $topic])
@includeWhen(Auth::check(),'topics._reply_box', ['topic' => $topic])
@include('topics._reply_list', ['replies' => $topic->replies()->with('user')->get()])
</div>
</div>

View File

@ -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']]);
Route::resource('replies', 'RepliesController', ['only' => ['store', 'destroy']]);