marcelgwerder / laravel-api-handler

Package providing helper functions for a Laravel REST-API
Other
152 stars 45 forks source link

Filter by relationship attribute #10

Open brunocascio opened 9 years ago

brunocascio commented 9 years ago

/user/dogs?breed.name=labrador

is it possible?

In this case:

dogs->belongsTo('Breed');

In breed, name is an attribute.

marcelgwerder commented 9 years ago

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

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

sptdigital commented 6 years ago

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 commented 6 years ago

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.

Pederytter commented 6 years ago

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

sptdigital commented 6 years ago

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();
scheMeZa commented 6 years ago

Would love to see this