Why are you calling collection on a ResourceCollection:
$emp = Employee::with(['salaries', 'designations'])->get();
return new EmployeeCollection($emp);
https://laravel.com/docs/9.x/eloquent-resources#resource-collections
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
Please or to participate in this conversation.