続いてコメントを一覧で取得する API を作成します.このために,コメントをまとめてコレクション形式で返す CommentCollection というリソースを作成します.
vagrant@ubuntu2204 CommentAPI $ php artisan make:resource CommentCollection ⏎
INFO Resource collection [app/Http/Resources/CommentCollection.php] created successfully.
vagrant@ubuntu2204 CommentAPI $
生成されたリソースを確認します.
app/Http/Resources/CommentCollection.php
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
class CommentCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
return parent::toArray($request);
}
}
次に一覧取得のためのルートを定義します.
routes/api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Resources\CommentResource;
use App\Http\Resources\CommentCollection;
use App\Models\Comment;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('/comments/{id}', function (string $id) {
return new CommentResource(Comment::findOrFail($id));
});
Route::get('/comments', function () {
return new CommentCollection(Comment::get());
});
次のコマンドでルートが正しく定義されていることを確認します.
vagrant@ubuntu2204 CommentAPI $ php artisan route:list ⏎ GET|HEAD / ......................................................... POST _ignition/execute-solution ignition.executeSolution › Spat… GET|HEAD _ignition/health-check ignition.healthCheck › Spatie\Larav… POST _ignition/update-config ignition.updateConfig › Spatie\Lar… GET|HEAD api/comments .............................................. GET|HEAD api/comments/{id} ......................................... GET|HEAD api/user .................................................. GET|HEAD sanctum/csrf-cookie sanctum.csrf-cookie › Laravel\Sanctum … Showing [8] routes vagrant@ubuntu2204 CommentAPI $
作成した API に接続してコメントを一覧で取得します.コメントを個別に取得した時と同様に,件名や本文が Unicode エスケープされています.さらに,CommentResourceで設定したとおり,投稿日時の属性は表示されず,更新日時のずれはすでに解消されています.つまり,これは CommentResource が利用されていることを意味しています.
C:\Users\Rinsaka>curl http://192.168.56.101:8000/api/comments/ ⏎ {"data":[{"id":1,"title":"\u6700\u521d\u306e\u30b3\u30e1\u30f3\u30c8","body":"\u6700\u521d\u306e\u30b3\u30e1\u30f3\u30c8\u3067\u3059\uff01","updated_at":"2023-10-02T10:10:10.000000Z"},{"id":2,"title":"2\u3064\u76ee","body":"2\u3064\u76ee\u306e\u30b3\u30e1\u30f3\u30c8\u3067\u3059\uff01","updated_at":"2023-10-02T10:20:10.000000Z"},{"id":3,"title":"<\u4e09\u500b\u76ee>\u306e\u30b3\u30e1\u30f3\u30c8","body":"\u30b7\u30fc\u30c0\u306b\u3088\u3063\u3066\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u8a2d\u5b9a\u3057\u307e\u3059\uff0e","updated_at":"2023-10-02T10:30:10.000000Z"}]} C:\Users\Rinsaka>
CommentCollection.php を修正して,Unicode エスケープを行わないようにします.この作業は前のページとほぼ同じです.
app/Http/Resources/CommentCollection.php
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
class CommentCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
return parent::toArray($request);
}
/**
* Customize the outgoing response for the resource.
*
* @param \Illuminate\Http\Request
* @param \Illuminate\Http\Response
* @return void
*/
public function withResponse($request, $response)
{
$response->header('Charset', 'utf-8');
$response->setEncodingOptions(JSON_UNESCAPED_UNICODE);
}
}
再び一覧を取得すると件名や本文を読むことができるようになりました.
C:\Users\Rinsaka>curl http://192.168.56.101:8000/api/comments/ ⏎
{"data":[{"id":1,"title":"最初のコメント","body":"最初のコメントです!","updated_at":"2023-10-02T10:10:10.000000Z"},{"id":2,"title":"2つ目","body":"2つ目のコメントです!","updated_at":"2023-10-02T10:20:10.000000Z"},{"id":3,"title":"<三個目>のコメント","body":"シーダによってテストデータを設定します.","updated_at":"2023-10-02T10:30:10.000000Z"}]}
C:\Users\Rinsaka>