Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ignisrzeus's avatar

How to Query using properties from a Resource

Hey guys! Noob question here. Would just like to know if it is possible to use the query builder to search for a certain property from its own Resource.

For Example:

Blood Component (Resource)

return [
            'id' => $this->id,
            'component' => $this->component,
            'amount' => $this->amount,
            'status' => $this->status,
            'blood_unit_id' => $this->blood_unit_id,
            'blood_unit' => new BloodUnitResource($this->whenLoaded('blood_unit')),
            'blood_type' => $this->blood_unit->typed_blood_unit->ABO,
            'expiration_date' => $this->getExpirationDate(),
            
        ];  

SInce blood type doesn't exist as a column for Blood Component, how can I let's say retrieve all Blood Components whose blood type is A+?

# I believe this will not work as the where will only look for columns, not the Resource
query->where();

Also, I do not want to get all blood components, then filter it in the client side. If it's possible, I'd really like to do it in the server side as it makes more sense to do it that way (and more efficient I guess). I've tried searching for answers to no avail.

Thanks Laracasts, hoping for your help!

0 likes
1 reply
Sinnbeck's avatar

Why not simply load it in the query you are performing before passing it to a resource? Can you perhaps show the query code?

Please or to participate in this conversation.