我正在尝试为我的应用程序创建一个 API,以便我可以共享端点并将一个应用程序作为具有业务逻辑的核心应用程序,而另一个可以连接到公开的端点以将功能作为服务使用。
当我尝试到达终点时出现错误。
下面是我的route/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());
});
我的资源类看起来像
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,
];
}
错误是
抱歉,找不到您要查找的页面。
最佳答案
使用api
前缀-
127.0.0.1:8000/api/cars
要转换资源集合,需要使用collection()方法-
return PostModellResource::collection(PostModell::all());
关于php - laravel 5.5 资源 restful API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48417116/