Skip to content

Commit

Permalink
话题回复
Browse files Browse the repository at this point in the history
  • Loading branch information
liyu001989 committed Aug 7, 2020
1 parent 4c66f02 commit a4e903a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
22 changes: 22 additions & 0 deletions app/Http/Controllers/Api/RepliesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Controllers\Api;

use App\Models\Topic;
use App\Models\Reply;
use Illuminate\Http\Request;
use App\Http\Resources\ReplyResource;
use App\Http\Requests\Api\ReplyRequest;

class RepliesController extends Controller
{
public function store(ReplyRequest $request, Topic $topic, Reply $reply)
{
$reply->content = $request->content;
$reply->topic()->associate($topic);
$reply->user()->associate($request->user());
$reply->save();

return new ReplyResource($reply);
}
}
13 changes: 13 additions & 0 deletions app/Http/Requests/Api/ReplyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Http\Requests\Api;

class ReplyRequest extends FormRequest
{
public function rules()
{
return [
'content' => 'required|min:2',
];
}
}
20 changes: 20 additions & 0 deletions app/Http/Resources/ReplyResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ReplyResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'user_id' => (int) $this->user_id,
'topic_id' => (int) $this->topic_id,
'content' => $this->content,
'created_at' => (string) $this->created_at,
'updated_at' => (string) $this->updated_at,
];
}
}
3 changes: 3 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
Route::resource('topics', 'TopicsController')->only([
'store', 'update', 'destroy'
]);
// 发布回复
Route::post('topics/{topic}/replies', 'RepliesController@store')
->name('topics.replies.store');
});
});
});

0 comments on commit a4e903a

Please sign in to comment.