dingo / api

A RESTful API package for the Laravel and Lumen frameworks.
BSD 3-Clause "New" or "Revised" License
9.33k stars 1.25k forks source link

include error Call to undefined relationship #1650

Closed mdeprezzo closed 5 years ago

mdeprezzo commented 5 years ago
Q A
Bug? yes
New Feature? no
Framework Lumen
Framework version 5.8.*
Package version 2.2.*
PHP version 7.2

Actual Behaviour

When i try to include something, that isn't a model relationship got that error. This is strange, because on dingo 2.0.0-alpha1 i do that, and all works fine.

Expected Behaviour

Return the data from custom includes

Steps to Reproduce

Define a custom include. Meaning, that the data to includes comes to the custom incude define on

$availableIncludes = [..]
specialtactics commented 5 years ago

Hey @mdeprezzo

Can you please provide a more substantial example? Code snippets appreciated.

mdeprezzo commented 5 years ago

@specialtactics sure, maybe I misunderstood the availableIncludes option. But, if i tried to include something that isn't a model relation i got that error. Example:

i've a custom method inside model that return an array of files from s3.

So i define that:

$availableIncludes = [
  'images'
]

public function includeImages(Model $model)
{
  return $model->getImages()
}
specialtactics commented 5 years ago

Hi @mdeprezzo

The includes actually get eventually passed to eloquent's with() function, so it won't work if it's just your own function that returns an array - they have to be actual relations.

Because you essentially just want to attach arbitrary data if a certain query param is set, so you can implement that logic yourself.

mdeprezzo commented 5 years ago

@specialtactics I'm sure, that in past version could add some custom includes, and i already did. Btw, thanks for reply