Tailor: Issue with using limit() and orderBy()

I have a blueprint Client:

fields:
    user_id:
        tab: General
        label: User
        type: recordfinder
        list: ~/plugins/rainlab/user/models/user/columns.yaml
        recordsPerPage: 10
        title: Find User
        descriptionFrom: email
        useRelation: false
        modelClass: RainLab\User\Models\User
        disabled: false
        span: left
        comment: The user for this coaching client.
    sessions:
        tab: General
        type: repeater
        label: Sessions
        displayMode: builder
        span: full
        prompt: Add a session
        form:
            fields:
                session:
                    type: mixin
                    source: Content\Sessions

in the theme, I retrieve the proper Client EntryRecord based on the current login User in order to retrieve the client sessions (as defined in the blueprint).

==
<?php
use Rainlab\User\Models\User;
use Tailor\Models\EntryRecord;

function onStart() {
    $user = Auth::user();
    $client = EntryRecord::inSection('Content\Clients')->where('user_id', $user->id)->with('sessions')->first();
    if ($client == null)
      return null;
    $this['client'] = $client;
}
?>
==
{% set sessions = client.sessions %}
{{ sessions|length }} -> print 2 which is correct

{% set sessions = sessions.limit(1).get() %}

{{ sessions|length }} -> print 0 which is incorrect

When I print the number of sessions, I have the expected value, in my case 2.
When I tried to limit the number of sessions to 1, it becomes 0.

Can’t we use the method limit on a collection from repeater ? How about orderBy() ?

Not sure if limit works on that particular collection. You may need to use a collection helper to limit it Collection - October CMS - 3.x

Or you can just slice it.

{{ sessions|slice(0,1) }}
1 Like

you nailed it! thanks @artistro08
I didnt think about this collection methods.

this works:

  {% set sessions = sessions.sortByDesc('date_time').splice(0, limit) %}
2 Likes