mehdi-fathi / eloquent-filter

Eloquent Filter is a package for filter data of models by the query strings. Easy to use and fully dynamic.
https://mehdi-fathi.github.io/eloquent-filter/
MIT License
430 stars 43 forks source link

Custom Detection Conditions Issue #211

Closed dungnh closed 1 month ago

dungnh commented 1 month ago

I have used custom detection conditions feature with old version and it worked well. But when I upgrade to 4.2.0 to work with Laravel 11, it got an error. I have setup and updated the classes for custom detection condition and I got the message: detect() cannot be called statically. The class implementing the eloquentFilter\QueryFilter\Detection\Contract\DetectorConditionContract interface and it cannot declare the detect method as static.

To Reproduce Steps to reproduce the behavior:

  1. My custom detection condition class like this:

use eloquentFilter\QueryFilter\Detection\Contract\DetectorConditionContract;

use Illuminate\Support\Collection;

class FilterConditionsDetector implements DetectorConditionContract { protected $detector;

public function __construct(array $detector = [])
{
    $this->detector = $detector;
}

public function detect($field, $params, $getWhiteListFilter = null, $HasOverrideMethod = true, $className = null): ?string
{
    if (in_array($field, [
        'custom_condition',
    ])) {
        $method = FilterRelatedQuery::class;
    }

    return $method ?? null;
}

public function setDetector(Collection $detector): void
{
    $this->detector = $detector;
}

public function getDetector(): Collection
{
    return $this->detector;
}

}


2. The FilterRelatedQuery class is: 

use eloquentFilter\QueryFilter\Queries\BaseClause; use Illuminate\Database\Eloquent\Builder;

class FilterRelatedQuery extends BaseClause { public function apply($query): Builder { if ($this->filter === 'custom_condition') { return ..... }

    return $query;
}

}

3. And when I call the filter method:

$user = new User(); $user->ignoreRequest(['page', 'sort', 'direction', 'keyword', 'column', 'operation'])->filter();


It got error: `detect() cannot be called statically`

Please help to check this. Thank you.
dungnh commented 1 month ago

I can fix my code now. The updated code and Readme of 4.2.0 version may need to update correctly. I just used to implement eloquentFilter\QueryFilter\Detection\Contract\ConditionsContract instead of eloquentFilter\QueryFilter\Detection\Contract\DetectorConditionContract

mehdi-fathi commented 1 month ago

@dungnh

Since I changed the name of the interface, that's right. I should have mentioned it in readme.