I have multiple resources and it makes things so much cleaner as my resources isnt littered with if's and such. I know exactly what to expect when using a specific resource.
Nov 7, 2019
2
Level 13
I just repeated myself in api resource how to DRY
public function toArray($request)
{
return [
'name' => $this->name,
'profile' => $this->when($request->fullUrl() == route('api.user.show',[$this->id,$this->slug]), $this->profile),
'path' => $this->path(),
'image' => $this->when($request->fullUrl() == route('api.user.show',[$this->id,$this->slug]), $this->profile)
];
}
I have one api resource and want to use it in different routes, The problem is i dont want to show some attributes in specific route, i used when method for this and it work but i think i repeated my self, How to clean this mess? Should i create new resource But if i do that is it ok that i have 2 resource for one model ?
Please or to participate in this conversation.