【发布时间】:2021-09-05 01:04:14
【问题描述】:
我正在尝试创建 Collection 以返回 Laravel 8 上 API 请求的特定字段。我的资源文件:
public function toArray($request) {
return [
'name' => $this->name
];
}
当我尝试获取所有用户时,我收到了这个错误:
Property [name] does not exist on this collection instance.
我的 api.php
Route::get('/users', function () {
return new UsersResource(User::all());
});
我是否需要在模型中写一些东西才能让它工作?
【问题讨论】:
-
模型是否在 $fillable 数组中包含名称?
标签: php laravel api collections resources