Add date to multiple field type
Created 2 years ago by cynoHi, 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.
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.
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?
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 %} ...
What is {{ dd(page.examples().paginate()) }}
by chance?
What about {{ dd(page.examples()) }}
? That should be a relation instance. From there we should be able to paginate. Assuming the relation is present.
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.
Excellent! Please mark answered for others if you would please 😄
@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);
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:This should 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.