laravel-api-handler
laravel-api-handler copied to clipboard
Filter by relationship attribute
/user/dogs?breed.name=labrador
is it possible?
In this case:
dogs->belongsTo('Breed');
In breed, name is an attribute.
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.
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?
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.
Yeah I'm having the same problem. A patch for this to work would be awesome.
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();
Would love to see this