When I load() relationships the links and metas disappear. Only the data remains.
If I remove the load('item', 'user') links and metas work but the relationship are not loaded.
Am I doing it wrong?
Controller
$messages = Message::where('user_id', $this->auth->user()->id)->paginate(2);
return MessageResource::collection($messages->load('item', 'user'));
Class MessageResource extends Resource
public function toArray($request)
{
return [
'id' => $this->id,
'from_id' => $this->from_id,
'to_id' => $this->to_id,
'content' => $this->content,
'item_id' => $this->item_id,
'created_at' => $this->present()->created_at,
'readed_at' => $this->readed_at,
'deleted_at' => $this->deleted_at,
'item' => new ItemResource($this->whenLoaded('item')),
'user' => new UserResource($this->whenLoaded('user'))
];
}
$this->load() returns an eloquent collection.
Try changing it to this..
$messages = Message::with('item', 'user')->where('user_id', $this->auth->user()->id)->paginate(2);
return MessageResource::collection($messages);
Correct thx
Most helpful comment
$this->load()returns an eloquent collection.Try changing it to this..