Query a collection with a custom relationship fieldType

I've created a custom relationship fieldType following this tutorial.

My custom field is a "Schools" fieldType and is mapped on a School::class eloquent model.

This works fine, now I can write posts and attach schools to them from the CP. If I look my posts .md files, eloquent ID of schools are correctly added to the file.

schools:
  - 8
  - 6
  - 5
  - 3
  - 2

Now I want to add a dropdown in my frontend to query posts related to a specific school.

So i tried this:

$qb = Statamic\Facades\Entry::query()->where('collection', 'posts');

if (request()->has('school_id') && request()->get('school_id')) {
    $qb->where('schools', request()->get('school_id'));
}

But it doesn't works because the where() and whereIn() methods of the query builder seems to be made to query on a single value property. it would have worked if my fieldType was a One to One relationship and my Yaml was like so:

school: 4

But in my case, it's a kind of hasMany relationship: a post could have MANY schools. I didn't found a way to query this kind of fieldType (with multiple ids).

Is there a way to achive this ?

Note: I don't use Antlers because I work on an existing Laravel application that use blade templating.

Thank you

>>>>>>> Unanswered <<<<<<<
0 Replies
1 Follower