chimit's avatar

[Fractal] How to include arbitrary data?

I have a PhotoTransformer with some includes. For example, Likes:

class PhotoTransformer extends TransformerAbstract
{
    protected $availableIncludes = [
        'likes',
    ];
    ...
    public function includeLikes(Photo $photo)
    {

Everything works without problems. But now I want to add a new type of includes - likescount:

class PhotoTransformer extends TransformerAbstract
{
    protected $availableIncludes = [
        'likes',
        'likescount',
    ];
    ...
    public function includeLikesCount(Photo $photo)
    {

But I get a response:

{
  "headers": {}
}

How can I include this arbitrary data into the Fractal Transformer?

0 likes
6 replies
accent-interactive's avatar

Have you tried spelling the method name with only the first include letter in uppercase?

public function includeLikescount 
chimit's avatar

Yes, I tried. The result is the same. It looks like I can include only really existing models.

pmall's avatar

No this is totally unrelated to existing models.

Please explain with more details what exactly you are doing. Are you sure you tell in the url you want to include them (they are available includes and not default includes).

1 like
chimit's avatar

When I use the original code:

class PhotoTransformer extends TransformerAbstract
{
    protected $availableIncludes = [
        'likes',
    ];
    ...
    public function includeLikes(Photo $photo)
    {

I get correct results: http://take.ms/Eash5

But if I want to rename it:

class PhotoTransformer extends TransformerAbstract
{
    protected $availableIncludes = [
        'likescount',
    ];
...
    public function includeLikescount(Photo $photo)
    {

I get this: http://take.ms/KNOnJ

I don't use default includes.

chimit's avatar
chimit
OP
Best Answer
Level 7

It was my mistake! I used lazy-eager loading, so when the model does not exist, it throws an exception. LOL)

Please or to participate in this conversation.