<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class UserCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
public function toArray($request)
{
// return parent::toArray($request);
// since we manually mapped the UserResouce and want to keep it standardize
return UserResource::collection($this->collection);
}
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class UserResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
// return parent::toArray($request);
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
];
}
}
public function index()
{
$users = User::all();
return (new UserCollection($users))
->additional([
'filters' => $filters,
'headers' => $headers,
])
;
}
public function show($user_id)
{
$user = User::find($user_id);
return (new UserResource($user))->additional(['token'=>$token]);
}