laravel-api-handler icon indicating copy to clipboard operation
laravel-api-handler copied to clipboard

Filter by relationship attribute

Open brunocascio opened this issue 10 years ago • 6 comments

/user/dogs?breed.name=labrador

is it possible?

In this case:

dogs->belongsTo('Breed');

In breed, name is an attribute.

brunocascio avatar Nov 17 '14 17:11 brunocascio

Right now unfortunately not. This is a feature I would like to have too though. However there are some problems respectively open questions.

  • I think having dots in a parameter name causes problems so we would need a replacement.
  • We can't filter the parent table the way Eloquent attaches relationships. We would for example need to attach a join for each filter param.
  • If we query the relationship with with it would also make sense if the filter only filters this list instead of defining if the parent is in the result or not. Both with the same param won't work of course.

Maybe you have a suggestion for e clean implementation. Would be happy to hear it. I unfortunately right now don't have the time.

marcelgwerder avatar Nov 17 '14 17:11 marcelgwerder

For the issue with dot's in the name, it is worth reading this thread.

Has there been any changes to the way relationships are handled in Eloquent, or are they still done in a similar way?

sptdigital avatar Oct 12 '17 14:10 sptdigital

Having created a workaround for the aforementioned issue, the final query is missing the join for any tables defined in the '_with'.

i.e. my.api/someobject/?_with=contract&contract.contractnumber-lk=1234 will report something along the lines of 'unknown field contract.contractnumber on table someobject'.

The _with is getting parsed, but is not getting used.

sptdigital avatar Oct 23 '17 16:10 sptdigital

Yeah I'm having the same problem. A patch for this to work would be awesome.

Pederytter avatar Oct 30 '17 16:10 Pederytter

Here's how I got around this issue ....

if(Input::has('relatedModelsField')) {
            $relatedModelsField= Input::get('relatedModelsField');
            $model = Model::whereHas('relatedModel', function ($query) use ($relatedModelsField) {
                $query->where('relatedModelsField', '=', $relatedModelsField);
            });
        } else {
            $model= new Model();
        }

        $builder = ApiHandler::parseMultiple($model, array(), Input::except('relatedModelsField'))->getBuilder();

sptdigital avatar Dec 27 '17 14:12 sptdigital

Would love to see this

scheMeZa avatar May 27 '18 13:05 scheMeZa