Skip to content
New issue

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

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

Already on GitHub? # to your account

Add meta per item in a collection #354

Closed
renspoesse opened this issue Jan 12, 2017 · 8 comments
Closed

Add meta per item in a collection #354

renspoesse opened this issue Jan 12, 2017 · 8 comments

Comments

@renspoesse
Copy link

renspoesse commented Jan 12, 2017

Using the setMeta function it's possible to set metadata for an entire collection,

return $this->collection($obj->categories, new CategoryTransformer())->setMeta(['foo' => 'bar']); 

However, I'd like to be able to set additional metadata per item in the collection. Would be nice if this could be done in the CategoryTransformer by using a $this->setMeta() method on the transformer to add meta for the item (?)

Any thoughts on this? Is it already possible someway?

@johannesschobel
Copy link
Contributor

You can add a meta key to the transformer. However, this will be built inside the normal data container. and is probably not, what you want..

@renspoesse
Copy link
Author

You're right, that's exactly what I'm trying to avoid here :)

@johannesschobel
Copy link
Contributor

Sorry for the late response - have you found a solution yet?

Eventually, this approach may help you..
You can use a

protected $defaultIncludes = ['meta'];

within your transformer.. Then implement respective method:

public function includeMeta(Object $object) {
   $meta = $object->getMeta();
   return $this->item($meta, new MetaTransformer());
}

Of course, you would need to implement the MetaTransformer as well. However, it is possible to write this transformer in a generic way to output all fields from the $meta object..

Using the $defaultIncludes automatically triggers respective function and appends the inner Transformer to the resultset..

Maybe this helps!? Obviously not the best solution, but eventually you can tackle your requirements?!
Cheers

@renspoesse
Copy link
Author

That would still append the inner result in the data object, right?

@johannesschobel
Copy link
Contributor

Honestly - I don't know.. I would say, it depends on the Serializer you (Docs) use. In my current project, I use the JsonApiSerializer..

This serializer would create a relationships attribute within each element. Within this relationships there would be an meta element, and within this element there would be an reference to respective meta object. (something like this - don't quote me on this one!)

Unfortunately, I am not aware, how other serializers will handle this. You will need to try it out on your own, i guess...

@renspoesse
Copy link
Author

Using the DataArraySerializer..

How about this comment by @jasonlewis about having a meta() method #18 ?

@samansmink
Copy link

Hi im following this issue as i'm currently trying to figure out i solution to this. Im working on a function that returns User Follow requests which can have different statusses (pending, accepted, etc). Ideally when doing a call to /user/{id}/followers we'd like to have a JSON output like:

{
"data": [
    {"data" : {USEROBJECT1}, "meta" : {USEROBJECT_META1}},
    {"data" : {USEROBJECT2}, "meta" : {USEROBJECT_META2}},
    {"data" : {USEROBJECT3}, "meta" : {USEROBJECT_META3}},
]
}

As far as i know currently its only possible the set the Meta for the whole collection, Are there any plans for this functionality in the future?

@greydnls
Copy link
Contributor

greydnls commented Feb 8, 2017

I'm working on a solution for this. I should have a PR up shortly.

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

No branches or pull requests

4 participants