Refine will be launching soon 🎉 Sign up for the early access list to stay up to date.
Refine for Laravel
A package by Hammerstone

Relationship Condition

Hammerstone Refine has not launched yet. Sign up for the early access list to stay up to date!

The Relationship Condition is an extension of the Numeric Condition and is for querying against the count of a related model.

1RelationshipCondition::make('comment_count', 'Number of Comments')
2 ->relationship('comments');

This condition has an id of comment_count, a display of Number of Comments and operates against the comments relationship of the model your initial query is based on.

With this condition in place, the user will now be able to select e.g. [Number of Comments] [is greater than] [5].

Adding Query Restrictions

In the scenario where you want to scope your relationship down, you can pass a closure to the restrict method.

1RelationshipCondition::make('approved_comments', 'Number of Approved Comments')
2 ->relationship('comments')
3 ->restrict(function($query) {
4 $query->where('approved', 1);
5 });
Code highlighting powered by torchlight.dev, a Hammerstone product.

Now instead of counting every related comment, it will only count comments where approved = 1.

You can use this to create multiple instances of the same relationship with slight variations:

1// Approved
2RelationshipCondition::make('approved_comments', 'Number of Approved Comments')
3 ->relationship('comments')
4 ->restrict(function($query) {
5 $query->where('approved', 1);
6 });
7 
8// Pending
9RelationshipCondition::make('pending_comments', 'Number of Pending Comments')
10 ->relationship('comments')
11 ->restrict(function($query) {
12 $query->whereNull('approved');
13 });
14 
15// Denied
16RelationshipCondition::make('denied_comments', 'Number of Denied Comments')
17 ->relationship('comments')
18 ->restrict(function($query) {
19 $query->where('approved', 0);
20 });

Using the Database Builder

The most common scenario for most filters is to return an instance of Eloquent\Builder from your initialQuery method. This is the class you get when you call e.g. Post::query().

If you are using the underlying Database\Builder class as your initial query you'll also need to set the model where the relationship can be found.

Let's take a look:

1class PostsFilter extends Filter
2{
3 public function initialQuery()
4 {
5 // If you're returning an instance of Database\Builder...
6 return DB::table('posts');
7 }
8 
9 public function conditions()
10 {
11 return [
12 RelationshipCondition::make('comment_count', 'Number of Comments')
13 ->relationship('comments')
14 // Then you need to tell Refine that the `comments`
15 // relationship you're referencing can be found
16 // on the `Post` Eloquent model.
17 ->on(Post::class)
18 ];
19 }
20}

By calling the on method of the Relationship Condition, you're informing Refine that the comments relationship lives on the Post model. Refine will then be able to use that information to derive the correct query and apply it to your base query.