Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Include Key Is Excluded If Include Function Returns null #491

Closed
MannikJ opened this issue Aug 8, 2019 · 10 comments
Closed

The Include Key Is Excluded If Include Function Returns null #491

MannikJ opened this issue Aug 8, 2019 · 10 comments

Comments

@MannikJ
Copy link

MannikJ commented Aug 8, 2019

I would like to include a belongsTo relation via fractal. If requested by the client, the key should always be present in the response object even if it is null.

public function includeUser(Model $model)
{
    if (!$model->user) {
        return null;
    }
    return $this->item($model->user, new UserTransformer);
}

In the above example the resulting array would not contain the user key, but the result should be like this:

[
    .
    .
    .
    'user' => null
    .
    .
    .
]

How can I convince fractal to include the key?

@matthewtrask
Copy link
Contributor

I think you could do a ternary statement there, and if your relationship is null, call $this->null() I believe, and that should set the key there. I can mess with this some later.

@MannikJ
Copy link
Author

MannikJ commented Aug 8, 2019

Wow ;)

There is a transformer function null()? That's exactly what I have missed. Ternary statement or not doesn't matter. If this function exists, thank you!

@matthewtrask
Copy link
Contributor

It should work. If you are adding a return type, use the ResourceInterface object and everything will work.

@matthewtrask
Copy link
Contributor

Im going to close this for now, if you need more help feel free to open this issue again :)

@MannikJ
Copy link
Author

MannikJ commented Aug 9, 2019

I still couldn't figure out how to do it 😒
Just returning $this->null() from the include function leads to an empty array included in the request, but that's not what I want. I already managed to do it by making the model parameter in the transformer function optional and return the empty array from there.

It should work. If you are adding a return type, use the ResourceInterface object and everything will work.

Can you give a bit more detailed explanation?

I also found this issue #469 which seems to deal with the same problem. How long do you think it will take until the package covers that feature out of the box?

@matthewtrask
Copy link
Contributor

I will see if I can get a working example later today.

@matthewtrask
Copy link
Contributor

so @MannikJ just so I understand you have a model such as users and you want to be able to include the belongsTo relationship such as roles and have the key roles appear even if its null, but $this->null() doesn't give you what you want?

@MannikJ
Copy link
Author

MannikJ commented Aug 9, 2019

Your example is not very good, because I would not model a hasMany relation like roles with belongsTo. In my case there is an entity that has a user_id attribute and a belongsTo(User::class) relation.
When I transform this model and user_id is null, I still get an empty array instead of null when I use $this->null().
An empty array would probably be correct, if it is some kind of to many relation that is represented by a collection.

public function includeUser(Model $model)
{
    return $model->user_id
        ? $this->item($model->user, new UserTransformer)
        : $this->null();
}

@matthewtrask
Copy link
Contributor

ok I will see what I can recreate.

@lpschz
Copy link

lpschz commented Jan 18, 2021

in the main transform method you can have 'user' => null
and in the include, simply return null too.

public function includeUser(Model $model)
{
    return $model->user_id
        ? $this->item($model->user, new UserTransformer)
        : null;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants