{
"id": 201,
"name": "user.view",
"sso_id": 6,
"tasks": [
{
"id": 2,
"pivot": {
"operation_id": 201,
"task_id": 2
}
}
]
},
{
"id": 306,
"name": "whitelistedUri.view",
"sso_id": 7,
"tasks": [
{
"id": 1,
"pivot": {
"operation_id": 306,
"task_id": 1
}
},
{
"id": 2,
"pivot": {
"operation_id": 306,
"task_id": 2
}
}
]
}
class OperationController {
public function all()
{
$operations = Operation::query()
->whereIn('name', ["whitelistedUri.view", 'user.view'])
->with('tasks')->get();
return new OperationCollection($operations);
}
}
class OperationCollection extends ResourceCollection
{
public $collects = OperationResource::class;
public function toArray($request)
{
return parent::toArray($request);
}
}
class OperationResource extends JsonResource
{
/**
* @var Operation
*/
public $resource;
public function toArray($request)
{
return [
'id' => $this->resource->id,
'name' => $this->resource->name,
'sso_id' => $this->resource->sso_id,
'tasks' => $this->resource->tasks->pluck('id'),
];
}
}
with(['tasks' => function ($query) {
$query->select('id');
}])