-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe1fa8a
commit 727a6c8
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Models\Topic; | ||
use Illuminate\Http\Request; | ||
use App\Http\Resources\TopicResource; | ||
use App\Http\Requests\Api\TopicRequest; | ||
|
||
class TopicsController extends Controller | ||
{ | ||
public function store(TopicRequest $request, Topic $topic) | ||
{ | ||
$topic->fill($request->all()); | ||
$topic->user_id = $request->user()->id; | ||
$topic->save(); | ||
|
||
return new TopicResource($topic); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Api; | ||
|
||
class TopicRequest extends FormRequest | ||
{ | ||
public function rules() | ||
{ | ||
return [ | ||
'title' => 'required|string', | ||
'body' => 'required|string', | ||
'category_id' => 'required|exists:categories,id', | ||
]; | ||
} | ||
|
||
public function attributes() | ||
{ | ||
return [ | ||
'title' => '标题', | ||
'body' => '话题内容', | ||
'category_id' => '分类', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class TopicResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return parent::toArray($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters