New: Learn Statamic on Laracasts!

We've retired the forum in favor of GitHub Discussions.

New conversations can be started on GitHub. Existing conversations will remain for a while longer.

Head over to GitHub →

Combining queries with stache and eloquent

Vic Luijkx April 16, 2021 by Vic Luijkx

Hi everyone,

We are migrating a website to Statamic which has many features build using Laravel and eloquent. This brings up loads of query-challenges which we are not sure how to approach.

Let's say that users can pin articles for later. The articles are stored in stache (which we enjoy using) and the users/pins are stored in the DB using eloquent. We did manage to get to this point:

$pinnedEntries = \Statamic\Facades\Entry::query()
    ->where('collection', 'articles')
    ->whereIn('id', auth()->user()->pins()->pluck('collection_id')->toArray())
    ->where('published', true)

We have to manage way more advanced cases however. Like ordering articles based on which is pinned last. Or showing dates on which articles were pinned.

We did manage to get it working by querying the pins first and then querying every Entry individually. This feels really grows though. I was wondering if you guys could push us into a better direction.

>>>>>>> Unanswered <<<<<<<
1 Reply