PHP code example of soyhuce / laravel-json-resources
1. Go to this page and download the library: Download soyhuce/laravel-json-resources library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
soyhuce / laravel-json-resources example snippets
class UserResource extends \Soyhuce\JsonResources\JsonResource
{
/**
* @return array<string, mixed>
*/
public function format(): array
{
return [
'id' => $this->id,
'email' => $this->email,
];
}
}
class UserResource extends \Soyhuce\JsonResources\JsonResource
{
/**
* @return array<string, mixed>
*/
public function format(): array
{
return [
'id' => $this->id,
'role' => $this->role->label,
];
}
}
class UserController
{
public function index(): \Illuminate\Http\Resources\Json\JsonResource
{
return UserResource::collection(User::all());
}
}