@brunitosessa it depends, if you will only need is_opened value in your JSON resource, then it's fine to leave it in BarResource class, otherwise if you will need to access this value in other places, then add an accessor/method in the model class.
Feb 28, 2020
3
Level 1
Laravel Api Resources
I'm having problems understanding API resources in laravel 5.8. Supose I have a model called "Bars", and an API resource called BarResource:
class BarResource extends JsonResource
{
public function toArray($request)
return [
'id' => $this->id,
'name' => $this->name,
]
}
}
Ok. That works fine. Now I need to pass to every call, if the bar is opened or not (I can easeily calculate that). Where should I put the function that check if the bar is opened or closed?
class BarResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'is_opened' => ??????
]
}
}
Thanks a lot!
Level 53
Please or to participate in this conversation.