searchWhere() in repeater fields

I have a question for the searchWhere() method in October. How would one search repeater fields if it only searches by column? Is there another method to find this data?

I’m using this in twig, and I want to search the repeater blocks that I have in Tailor, but I don’t see any documentation on the twig function. I should note that I don’t want to use a collection for this. It needs to be paginated.

Is the repeater field stored in long json string?
searchWhere should be working with that most probably.

Since you know the json repeater structure in advance, there is also the possibility to use whereJsonContains like this ::whereJsonContains('destinations',["Goa"])->get()

Official doc Database: Query Builder - Laravel - The PHP Framework For Web Artisans

The issue I’m running into is trying to find the column to search in

check this thread, you could use the OFFLINE SiteSearch which is already handling search within Tailor

I ended up going a different route. I created a foreach loop to query the content. I also used searchWhere to get the initial page ids to make sure it’s correct

        {% set matchingContent = collect() %}
        {% set matchingPages = pages.searchWhere(searchTerms.term, ['title', 'content']) %}
        {% for page in matchingPages.get() %}
            {% if page.id not in matchingContent %}
                {% do matchingContent.push(page.id) %}
            {% endif %}
        {% endfor %}
        {% for page in pages.get() %}
            {% for section in page.builder %}
                {% if searchTerms.term|lower in section.content|lower %}
                    {% if page.id not in matchingContent %}
                        {% do matchingContent.push(page.id) %}
                    {% endif %}
                {% endif %}
            {% endfor %}
        {% endfor %}

Looks like we need a searchWhereRelation method for maximum performance. For example, chaining with whereRelation looks something like this…

{% set matchingPages = pages
    .searchWhere(searchTerms.term, ['title', 'content']
    .orWhereRelation('builder', 'content', 'LIKE', '%'~searchTerms.term~'%')
%}

However, the above solution will be a case sensitive search which is undesirable.

The proposed code we should introduce is:

{% set matchingPages = pages
    .searchWhere(searchTerms.term, ['title', 'content']
    .orSearchWhereRelation(searchTerms.term, 'builder', ['content'])
%}
2 Likes

I just learned something new today! That searchWhereRelation would be handy.

1 Like

This has been added and documented, ready for v3.2.15

2 Likes

I can see the only downside to using this is trying to parse json data. in the orWhereReleation method, I can, but it’s case sensitive and doesn’t allow you to lowercase the the value

EDIT: Nevermind, Saw how the function works and it works beautifully

1 Like