Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use filter set up in relationship's filter #69

Closed
kyleweishaupt opened this issue Jun 5, 2018 · 2 comments
Closed

Use filter set up in relationship's filter #69

kyleweishaupt opened this issue Jun 5, 2018 · 2 comments

Comments

@kyleweishaupt
Copy link

kyleweishaupt commented Jun 5, 2018

I'm trying to filter my cases model by patient, in which both have their own filters respectively. In my patient filter, I have the following filter I'd like to re-use if possible:

class PatientFilter extends ModelFilter
{
    public function name($name)
    {
        return $this->where('first_name', 'LIKE', DB::getPdo()->quote($name . '%'))
            ->orWhere('last_name', 'LIKE', DB::getPdo()->quote('%' . $name . '%'))
            ->orWhereRaw('concat([first_name], \' \', [last_name]) LIKE ' . DB::getPdo()->quote('%' . $name . '%'));
    }
}

I'm trying to access this filter from within my CaseEntityFilter class so I can pass something like patient_name as a filter on the case and return cases matching the patient name.

Here's what I've tried for the filter on cases, but to no luck:

class CaseEntityFilter extends ModelFilter
{
    // Cant seem to get this to point to the method on the actual filter
    public $relations = [
        'patient' => [
            'name',
            'firstName',
            'patient_name'
        ]
    ];

    // Produces 'invalid column' error
    public function patientName($name)
    {
        return $this->related('patient', 'name', $name);
    }

My database (SQL Server) throws an error about the column name not existing (which it rightfully doesn't). I can't seem to figure out how to execute the name() method on my patient's filter instead of just querying for the column 'name'.

I'm new to Laravel so is there something I'm missing or would this be considered a feature request?

Thank you for the excellent plugin by the way! :)

@Tucker-Eric
Copy link
Owner

They way you have this set up, if you pass a name parameter to your CaseEntityFilter it will automatically forward that to the name method on PatientFilter because name is present in your $relations['patient'] array. That array is also setup to forward a firstName parameter to PatientFilter::firstName() (if it exists) and patient_name to PatientFilter::patientName() (if it exists) without having to define any methods on CaseEntityFilter.

If you want to use a patient_name parameter to call a patientName() method on your CaseEntityFilter that will call the name() on PatientFilter then you can do it like:

class CaseEntityFilter extends ModelFilter
{
    // Cant seem to get this to point to the method on the actual filter
    public $relations = [
        'patient' => [
            'name',
            'firstName'
        ]
    ];

    public function patientName($name)
    {
        return $this->related('patient', function($query) use ($name) {
            // $query here is an instance of the `patient` relation's query builder
            return $query->filter(['name' => $name]);
        });
    }

@kyleweishaupt
Copy link
Author

Aha. The $query->filter() portion was exactly what I was looking for. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants