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

matalina's avatar

GUID in API Resources

I'm using MSSQL in Ubuntu 16.04LTS PHP 7.2. (I don't know the version of MSSQL)

I have a Model, Brand and Status table in the MSSQL database.

I'm writing an API for read only access to this database so I'm using the API Resource to convert my models to JSON easily.

I call the brand url and I'm able to pull up the Brand GUID without a problem.

I call the model url and eager loading brand using with on the query and the whenLoaded in the resource however the brand will not load with it despite being on the model correctly.

As a note the GUID requires a conversion using getBrandIDAttribute in order to display in a human readable format (This is likely the problem but no idea how to fix it).

When loading a resource through another resource as a relationship it's not using the get mutator.

The same exact code is working for the status relationship which uses an integer id to signify the relationship.

So the question is can the GUID be made human readable without the conversion? I've been unable to find the way to do it so just convert it with a mutator.

Or am I doing something wrong I'm completely missing??

Below are the snippets of code.

Model Resource

class Model extends JsonResource
{
    public function toArray($request)
    {
        return [
            'ModID' => $this->ModID,
            'ModelNumber' => $this->ModelNumber,
            'ModelDescription' => $this->ModelDescription,
            'PCID' => $this->PCID,
            'BrandID' => $this->BrandID,
            'MfrID' => $this->MfrID,
            'ModStatID' => $this->ModStatID,
            'Life' => $this->Life,
            'UPCNumber' => $this->UPCNumber,
            'DefaultReceiveAs' => $this->DefaultReceiveAs,
            'DisAllowBundle' => $this->DisAllowBundle,
            'ShowAPI' => $this->ShowAPI,
            'ImageURL' => $this->ImageURL,
            'ModelDetails' => $this->ModelDetails,

            'brand' => $this->whenLoaded('brand'),
            //'product_code' => $this->whenLoaded('product_code'),
            //'vendors' => Vendor::collection($this->whenLoaded('vendors')),
            //'manufacturer' => $this->whenLoaded('manufacturer'),
            'status' => $this->whenLoaded('status'),
            //'rates' => Rates::collection($this->whenLoaded('rates')),
        ];
    }
}

Brand Resource

class Brand extends JsonResource
{
    public function toArray($request)
    {
        return [
            'BrandID' => $this->BrandID,
            'BrandName' => $this->BrandName,
            'InActive' => $this->InActive,
        ];
    }
}

Status Resource

class ModelStatus extends JsonResource
{
    public function toArray($request)
    {
        return [
            'ModStatID' => $this->ModStatID,
            'Description' => $this->Description,
        ];
    }
}

Model Model

class Model extends VersiRentModel
{
    use SearchByScope;
    use SearchThroughScope;

    protected $table = 'Model';
    protected $primaryKey = 'ModID';
    public $incrementing = false;
    protected $fillable = [];

    // Relationships
    public function brand()
    {
        return $this->belongsTo(Brand::class,'BrandID','BrandID');
    }

    // ...

    public function status()
    {
        return $this->belongsTo(ModelStatus::class,'ModStatID','ModStatID');
    }

    // ...
}
0 likes
2 replies
matalina's avatar

As a side note: this is the error

"Malformed UTF-8 characters, possibly incorrectly encoded"

which is because of the BrandID byte codes displayed.

matalina's avatar
matalina
OP
Best Answer
Level 2

Found my answer.

In order to get the resource to populate. In the Model Resource file change brand in ToArray to

'brand' => new Brand($this->whenLoaded('brand')),

Please or to participate in this conversation.