christopherarter / laravel-resourceable
一个方便的方法,用于将模型值作为API资源访问
dev-master
2020-03-11 22:15 UTC
Requires
- php: ^7.1.3
- illuminate/config: 5.8.15|^6.0|^7.0
- illuminate/support: 5.8.15|^6.0|^7.0
Requires (Dev)
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2024-09-12 08:15:31 UTC
README
Laravel Resourceable
Laravel Resourceable 是一个包,允许您快速从 Api Resource 类中访问模型的数组值。
安装
composer require christopherarter/laravel-resourceable
示例
$user = new User([...]);
return $user->toResourceArray();
此包会自动在 App\Http\Resources
命名空间中查找定义为 ClassName + Resource 的资源类。因此,对于我们的用户示例,它会解析为 App\Http\Resources\UserResource
。
您还可以使用来自任何命名空间的自定义 API 资源类作为参数
return $user->toResourceArray(AlternateResource::class);
为什么?
这允许对端点进行非常精确的 API HTTP 测试,例如
// UserTest.php
$user = factory(\App\User::class)->create();
$this->get(route('users.show'))
->assertJson($user->toResourceArray());
贡献者
此包由 Chris Arter 编写,Dan Alverez 提供了宝贵的贡献。[链接](https://github.com/bayareawebpro)