Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Amrith's avatar

Empty Output From Resource

I m going to build up RESTful API, So i added php artisan make:resource EmployeeCollection now it gives following output there is no results in this output, please help me to resolve this incident.

EmployeeController.php

 public function update(EmployeeRequest $request, $id)
    {
        $emp = Employee::with(['salaries', 'designations'])->get();
        $emp = EmployeeCollection::collection($emp);
        return $emp;

    }

EmployeeCollection.php

   public function toArray($request)
    {
        return [
            'salaries' => $this->collection

Model/Employee.php

class Employee extends Model
{
    use HasFactory;

  .......

    public function salaries(){
        return $this->hasMany(Salary::class,'emp_no');
    }
}

Model/Salary.php

class Salary extends Model
{
....
    public function employee(){
            return $this->belongsTo(Employee::class,'emp_no');
    }


}

Output

PUT http://127.0.0.1:8000/api/v1/emp/2

HTTP/1.1 200 OK
Host: 127.0.0.1:8000
Date: Tue, 15 Feb 2022 10:13:35 GMT
Connection: close
X-Powered-By: PHP/8.0.13
Cache-Control: no-cache, private
Date: Tue, 15 Feb 2022 10:13:35 GMT
Content-Type: application/json
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59
Access-Control-Allow-Origin: *

{
  "data": [
    {
      "salaries": {
        "connection": {},
        "grammar": {},
        "processor": {},
        "bindings": {
          "select": [],
          "from": [],
          "join": [],
          "where": [],
          "groupBy": [],
          "having": [],
          "order": [],
          "union": [],
          "unionOrder": []
        },
        "aggregate": null,
        "columns": null,
        "distinct": false,
        "from": "employees",
        "joins": null,
        "wheres": [],
        "groups": null,
        "havings": null,
        "orders": null,
        "limit": null,
        "offset": null,
        "unions": null,
        "unionLimit": null,
        "unionOffset": null,
        "unionOrders": null,
        "lock": null,
        "beforeQueryCallbacks": [],
        "operators": [
          "=",
          "<",
          ">",
          "<=",
          ">=",
          "<>",
          "!=",
          "<=>",
          "like",
          "like binary",
          "not like",
          "ilike",
          "&",
          "|",
          "^",
          "<<",
          ">>",
          "&~",
          "rlike",
          "not rlike",
          "regexp",
          "not regexp",
          "~",
          "~*",
          "!~",
          "!~*",
          "similar to",
          "not similar to",
          "not ilike",
          "~~*",
          "!~~*"
        ],
        "useWritePdo": false
      }
    }
  ]
}

Response code: 200 (OK); Time: 242ms; Content length: 715 bytes

0 likes
14 replies
Amrith's avatar

@tykus Confused

class EmployeeResource extends JsonResource
class EmployeeCollection extends ResourceCollection

Can someone explain the difference between a ResourceCollection and JsonResource?

tykus's avatar

@Amrith yeah, the docs can do that

TLDR; a Resource class defines the structure of a single Resource (model); a ResourceCollection class defines the structure of a Collection of Resources

1 like
Amrith's avatar

@tykus Thanks a lot .Sorry documentation also confused for me. please help me clarifiy this. I define $emp , then it returns collections . am i right ? SO 1st code is CORRECT . please advise me

1st code

  $emp = Employee::with(['salaries', 'designations'])->get();
$emp = EmployeeResource::collection($emp);

2nd Code

 $emp = Employee::with(['salaries', 'designations'])->get();
$emp = new EmployeeResource($emp);
tykus's avatar

@Amrith you really only every need a ResourceCollection class if you want to modify the data that surrounds the Collection; otherwise, a Resource class will be enough. Passing a query result (Collection) to the collection method will suffice:

$emp = Employee::with(['salaries', 'designations'])->get();
$emp = EmployeeResource::collection($emp);

where

class EmployeeResource extends JsonResource
1 like
Amrith's avatar

@tykus Thanks ! Now Understood. Thanks again and again . Here is my last question

In EmployeeResource class i m using following code

 'salaries' => SalaryResource::collection($this->whenLoaded('salaries')),

Also this code is working well.

 'salaries' => $this-salaries,

What is main diff between these two codes ?

tykus's avatar

@Amrith the whenLoaded method will prevent lazy-loading the salaries and remove the salaries key from the result. In the second case, you will always lazy-load the salaries relation when it is not already present.

1 like
Amrith's avatar

@tykus What is the main benefit of $this->whenLoaded('salaries')

What is the meaning key of salaries Its not removed in my results . see it.

  'salaries' => SalaryResource::collection($this->whenLoaded('salaries')),
"data": [
    {
      "id": 1,
      "emp_no": 2,
      "birth_date": "1987-11-26",
      "first_name": "Abiman",
      "last_name": "J",
      "gender": "m",
      "hire_date": "2020-02-15",
      "salaries": [
        {
          "emp_no": 2,
          "salary": 90000,
          "from_date": "2020-01-01",
          "to_date": "2020-01-01"
        }
      ],
tykus's avatar

@Amrith you have eager-loaded salaries, so salaries is loaded

Employee::with(['salaries', 'designations'])->get();

What is the main benefit of $this->whenLoaded('salaries')

Already explained this...

the whenLoaded method will prevent lazy-loading the salaries and remove the salaries key from the result

1 like
Amrith's avatar

@tykus you said that "...............remove the salaries key from the result"

What is that ? i can see "salary" key in this output.

"data": [
    {
      "id": 1,
      "emp_no": 2,
      "birth_date": "1987-11-26",
      "first_name": "Abiman",
      "last_name": "J",
      "gender": "m",
      "hire_date": "2020-02-15",
      "salaries": [
        {
          "emp_no": 2,
          "salary": 90000,
          "from_date": "2020-01-01",
          "to_date": "2020-01-01"
        }
      ],
tykus's avatar

@Amrith try it without eager-loading the salaries relation:

Employee::with(['designations'])->get();
1 like
ehab.aboshehab's avatar

@amrith Your problem is in the EmployeeCollection.php

public function toArray($request)
    {
        return [
            'salaries' => $this->collection

It should be :

public function toArray($request)
    {
        return [
            'salaries' => $this->salaries
1 like

Please or to participate in this conversation.