laravel 5.5资源restful API

DEN*_*NJI 6 php rest laravel laravel-5 laravel-5.5

我正在尝试为我的应用程序创建一个API,以便我可以共享端点并将一个应用程序作为具有业务逻辑的核心应用程序,另一个可以与暴露的端点连接以将该功能用作服务.

我尝试命中端点时收到错误.

下面是我的路线/ api.php

<?php

use App\PostModell;
use App\Http\Resources\PostModellResource;
use Illuminate\Http\Request;

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});
Route::get( '/cars',function(){
    return new PostModellResource(PostModell::all());
}); 
Run Code Online (Sandbox Code Playgroud)

我的资源类看起来像

class PostModellResource extends Resource
{
         public function toArray($request)
    {
        return
        [
            'id'=>$this->id,
            'title'=>$this->title,
            'body'=>$this->body,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,

        ];          
    }
Run Code Online (Sandbox Code Playgroud)

错误是

Sorry, the page you are looking for could not be found.

Soh*_*415 3

使用api前缀-

127.0.0.1:8000/api/cars 
Run Code Online (Sandbox Code Playgroud)

要转换资源集合,您需要使用collection()方法 -

return PostModellResource::collection(PostModell::all());
Run Code Online (Sandbox Code Playgroud)