Add date to multiple field type
Created 5 months ago by cyno

Hi, I want to add timestamps to multiple relationship fields. For example a user is buying a product, when it happens its added to the users purchased multiple field, so he/she can have access to purchased products later. At this moment I wanna also store when the purchase happened. Is there any way to do this? Also can I show the data in twig {{ user().purchased_product.created_at }} or something? Forgot to mention that I can do this by modifying the field type schema, to have timestamps, and write a presenter, that gets the data and passes it to the view controller. But I feel like this is quite brute force, and not Pyro style at all, also I wanna avoid changing the field type.

ryanthompson  —  4 months ago Best Answer

Ok - I think this is because pages maps custom entry stuff automatically but we need to reach around that magic. Forgot about entry / type pattern here.

This should be your relation (assuming example is the field slug:

{{ dd(page.entry.examples()) }}

This should paginate:

{{ dd(page.entry.examples().paginate()) }}

The relation is not on the page it's on the page > entry which is the whole "type pattern" thing with pages and posts.

ryanthompson  —  5 months ago

Currently there is no way to override timestamp / meta information in the pivot table itself. However multiple relates to another stream where you will have that information. Sounds like you might need a stream called "purchases" and make a manual relationship to that. So when a purchase is made - the purchase information is saved and you can use hooks for users to add a relationship: https://github.com/anomalylabs/reactions-extension/blob/master/src/ReactionsExtensionServiceProvider.php#L54-L78

Then you could do something like {% foreach purchase in user().purchases) %} and output the time like purchase.created_at for example.

cyno  —  5 months ago

Thank you for the answer, sad to hear i cannot override information in the pivot table. I will go with the stream method u suggested. I have another question, gonna ask it here, if needed I can move it to a separate topic. When I'm having a repeater field on a page and I go like {% for example in page.examples %} how can I have pagination on this?

ryanthompson  —  4 months ago

I believe what you after would be like this:

{% set examples = page.examples().paginate() %}

{{ examples.links|raw }} or {{ examples.render|raw }} // I believe both work

{% for example in examples %} ...
cyno  —  4 months ago

Hmmm I've tried it with both solutions, but cannot make it work. I initially tried the same but with examples.render(). Mabye I'm just missing something.

ryanthompson  —  4 months ago

What is {{ dd(page.examples().paginate()) }} by chance?

cyno  —  4 months ago

Sadly I left work for today can check it tomorrow (in 16 hours).

cyno  —  4 months ago

Just gives null.

ryanthompson  —  4 months ago

What about {{ dd(page.examples()) }}? That should be a relation instance. From there we should be able to paginate. Assuming the relation is present.

cyno  —  4 months ago

So {{ dd(page.examples()) }} gives null as well. However {{ dd(page.examples) }} works fine. Problem is it gives an EntryCollection back, with the repeater field entries, which I can't paginate.

ryanthompson  —  4 months ago Best Answer

Ok - I think this is because pages maps custom entry stuff automatically but we need to reach around that magic. Forgot about entry / type pattern here.

This should be your relation (assuming example is the field slug:

{{ dd(page.entry.examples()) }}

This should paginate:

{{ dd(page.entry.examples().paginate()) }}

The relation is not on the page it's on the page > entry which is the whole "type pattern" thing with pages and posts.

cyno  —  4 months ago

Oh, I see now, completely forgot about this type thing, already had problems with it, when tried to extend the pagemodul. Works perfectly now, thank you for investigating, and solving this.

ryanthompson  —  4 months ago

Excellent! Please mark answered for others if you would please 😄

cyno  —  4 months ago

@ryanthompson Not sure if this is the best place, but in case anyone has this same problem, here is the solution, feel free to edit or move it. So I wanted to have a search query on repeater fields, and have the results paginated. Took quite some time to figure out, till i reached for good ol' laravel.

$paginated_examples= $page->getEntry()->examples() ->where(function ($query) use ($search_key) { $query->where('field_1', 'LIKE', '%' . $search_key . '%') ->orWhere('field_2', 'LIKE', '%' . $search_key . '%'); })->paginate(1);