laravel-learn-bbs/app/Http/Controllers/Api/RepliesController.php
2018-06-10 22:37:37 +08:00

51 lines
1.4 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Requests\Api\ReplyRequest;
use App\Models\Reply;
use App\Models\Topic;
use App\Models\User;
use App\Transformers\ReplyTransformer;
class RepliesController extends Controller
{
public function store(ReplyRequest $request, Topic $topic, Reply $reply)
{
$reply->content = $request->get('content');
$reply->topic_id = $topic->id;
$reply->user_id = $this->user()->id;
$reply->save();
return $this->response->item($reply, new ReplyTransformer())->setStatusCode(201);
}
/**
* @param Reply $reply
* @return \Dingo\Api\Http\Response
* @throws \Illuminate\Auth\Access\AuthorizationException
* @throws \Throwable
*/
public function destroy(Reply $reply)
{
$this->authorize('destroy', $reply);
$reply->delete();
return $this->response->noContent();
}
/**
* @param Topic $topic
* @return \Dingo\Api\Http\Response
*/
public function index(Topic $topic)
{
$replies = $topic->replies()->recent()->paginate(20);
return $this->response->paginator($replies, new ReplyTransformer());
}
public function userIndex(User $user)
{
$replies = $user->replies()->recent()->paginate(20);
return $this->response->paginator($replies, new ReplyTransformer());
}
}