Skip to content

Commit

Permalink
用户可以发表评论
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Sep 20, 2019
1 parent 10b4594 commit aaec05b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 77 deletions.
57 changes: 16 additions & 41 deletions app/Http/Controllers/RepliesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,30 @@
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\ReplyRequest;
use Auth;

class RepliesController extends Controller
{
public function __construct()
{
$this->middleware('auth', ['except' => ['index', 'show']]);
$this->middleware('auth');
}

public function index()
{
$replies = Reply::paginate();
return view('replies.index', compact('replies'));
}

public function show(Reply $reply)
public function store(ReplyRequest $request, 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 = Reply::create($request->all());
return redirect()->route('replies.show', $reply->id)->with('message', 'Created successfully.');
}

public function edit(Reply $reply)
{
$this->authorize('update', $reply);
return view('replies.create_and_edit', compact('reply'));
}
$reply->content = $request->content;
$reply->user_id = Auth::id();
$reply->topic_id = $request->topic_id;
$reply->save();

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.');
}
return redirect()->to($reply->topic->link())->with('success', '评论创建成功!');
}

public function destroy(Reply $reply)
{
$this->authorize('destroy', $reply);
$reply->delete();
public function destroy(Reply $reply)
{
$this->authorize('destroy', $reply);
$reply->delete();

return redirect()->route('replies.index')->with('message', 'Deleted successfully.');
}
}
return redirect()->route('replies.index')->with('success', '评论删除成功!');
}
}
30 changes: 1 addition & 29 deletions app/Http/Requests/ReplyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,9 @@
class ReplyRequest extends Request
{
public function rules()
{
switch($this->method())
{
// CREATE
case 'POST':
{
return [
// CREATE ROLES
];
}
// UPDATE
case 'PUT':
case 'PATCH':
{
return [
// UPDATE ROLES
];
}
case 'GET':
case 'DELETE':
default:
{
return [];
}
}
}

public function messages()
{
return [
// Validation messages
'content' => 'required|min:2',
];
}
}
11 changes: 6 additions & 5 deletions app/Observers/ReplyObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

class ReplyObserver
{
public function creating(Reply $reply)
public function created(Reply $reply)
{
//
$reply->topic->reply_count = $reply->topic->replies->count();
$reply->topic->save();
}

public function updating(Reply $reply)
public function creating(Reply $reply)
{
//
$reply->content = clean($reply->content, 'user_topic_body');
}
}
}
13 changes: 13 additions & 0 deletions resources/views/topics/_reply_box.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@include('shared._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 mr-1"></i> 回复</button>
</form>
</div>
<hr>
2 changes: 1 addition & 1 deletion resources/views/topics/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
{{-- 用户回复列表 --}}
<div class="card topic-reply mt-4">
<div class="card-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>
Expand Down
4 changes: 3 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@

Route::get('topics/{topic}/{slug?}', 'TopicsController@show')->name('topics.show');

Route::resource('replies', 'RepliesController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]);
Route::resource('replies', 'RepliesController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]);

Route::resource('replies', 'RepliesController', ['only' => ['store', 'destroy']]);

0 comments on commit aaec05b

Please sign in to comment.