Level 18
@ranx99 try this
Post::where('id', $id)->select('id', 'title', 'subtitle')->firstOrFail();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am trying to find out if a property was selected, if it was selected.. merge it with the resource:
Post::where('id', $id)->select(['id', 'title', 'subtitle'])->firstOrFail()
Then in the api resouce
class PostResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->when(property_exists($this, 'title'), $this->title),
'subtitle' => $this->when(property_exists($this, 'subtitle'), $this->subtitle)
// Other fields and relations
];
}
}
But property_exists doesnt work, it always return false.
Please or to participate in this conversation.