Tucker-Eric / EloquentFilter

An Eloquent Way To Filter Laravel Models And Their Relationships
http://tucker-eric.github.io/EloquentFilter
MIT License
1.72k stars 120 forks source link

Unable to get relation setup to trigger #97

Closed eokorieFA closed 5 years ago

eokorieFA commented 5 years ago

First of all, fantastic package. I have been using this for a good while and it has made life so much easier when building a search functionality into my app. However I am having an when trying to filter records via a relation.

Take the following:. I am trying to create a user search functionality and trying to add an option where the users records can be displayed in the search results based on their privacy setting. I am however finding that the privacySettingsSetup is not being triggers therefore the results always come back the same.

// User Model
class User extends Model {
   public function modelFilter()
    {
        return $this->provideFilter(UserFilter::class);
    }

    public function privacySettings()
    {
        return $this->hasMany(UserPrivacySetting::class);
    }
}

// User Filter
class UserFilter extends ModelFilter
{
    public function privacySettingsSetup($query)
    {
        return $query->isDisplayedInSearchResults();
    }
}

// User Privacy Setting Model
class UserPrivacySetting extends Model
{
    use Filterable;

    protected $fillable = [
        'user_id',
        'key',
        'value'
    ];

    public function modelFilter()
    {
        return $this->provideFilter(UserPrivacySettingsFilter::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeIsDisplayedInSearchResults() {
        return $this->where('key', '=', 'show_in_search_results')->where('value', '=',1);
    }
}

// User Privacy Setting Filter
class UserPrivacySettingsFilter extends CoronaModelFilter
{
     public $relations = [];
}

Is there something that I am missing here to get this to work the way I would like it to?

Thanks in advance,

Emmanuel

Tucker-Eric commented 5 years ago

Hey! Appreciate the positive feedback!!

So, that method will be called in UserFilter before queries to the privacySettings relation are applied from either the $relations array or the relation filter method which means if there are no queries to that relation the setup method will not be called.

If you are trying to apply this by default without any queries to that relation that could explain why it's not working.

One trick to ensure that's called regardless of input would be to call the relation method in a setup method on the user filter with an empty closure to ensure that relation is always setup.

public function setUp()
{
    $this->relation ('privacySettings', function() {});
}
eokorieFA commented 5 years ago

Awesome stuff.. I have managed to get it working with this:

public function setUp() {
       $this->related('privacySettings', static function ($query) {
            $query->where('key', '=', 'show_in_search_results')->where('value', '=', 1);
        });
}

And it is working as expected... Thanks for putting me on the right path and once again... great package 👍