Open bbprojectnet opened 2 years ago
When you're specifying it as an include path it'll be using the schema to check that the include path is valid. So you'd need this as a relationship on the schema, so it knows to accept it as a valid include path.
This is going to need some sort of new class for the relationship type. I'm not sure how complex that is though. How are you currently loading the data for that? I.e. how are you avoiding n+1 problems?
According to https://laraveljsonapi.io/docs/2.0/resources/relationships.html#specifying-data it should works as i expected ;)
Not true, that doesn't envisage you are loading the data via an include path. I.e. what you're trying to do isn't something I'd envisaged as a use case. If you want things to be eager loaded (i.e. include paths), it needs to be via Eloquent relationships because that's how eager loading works on models.
Ok, thanks for the clarification :)
How are you currently loading the data for that? I.e. how are you avoiding n+1 problems?
Yes, that would probably be another problem I would encounter.
But i can't understand what is the real use case of that:
$this->relation('author')->withData($this->someOtherValue)
? It is not visibile but default, but also can't by included by include if relationship dosn't exists - but, if exists, whats the point of withData()
? ;)
Sorry for that maybe silly question, i'm trying better understand concept of rewriting relationships in resource class.
What else bothers me, if i choose declaring relationship in resource, rather than schema, how to achieve for example readOnly()
which is available in schema class? What happens when i declare relationship in both schema and resource class?
What is the best way to create virtual relationship like in previous version (queriesOne
, queriesMany
)?
Back to the main problem, i definitely need convert my method to relationship :) - maybe you can point me in right direction, how to achieve lastestOfMany()
but for BelongsToMany, it's only available for HasOne :(
Sorry for that many questions, thanks for all!
It's mainly there for non-Eloquent resources: the resource class isn't designed to work specifically with Eloquent, it's for anything.
If using with an Eloquent model, you really need to use the Eloquent relationships as then you get the eager loading, which is what include
query parameters are.
I'm not sure what you're trying to do - my stock response for this kind of thing is work out what the query would be if executing directly on the model. Then I can work out how to translate that to the JSON:API schema and/or whether I need to add something to support a specific eager loading pattern that works directly on a model. The Laravel docs about eager loading include info on constraining eager loads etc: https://laravel.com/docs/9.x/eloquent-relationships#eager-loading
Hi,
I have "virtual" relationship declared as:
in my resource class. It's works fine when i invoke
alwaysShowData()
, but, when i want to include this relation via include query param i get:why jsonapi trying to get data from model when i declared withData()? How to make it works without
alwaysShowData()
?According to https://laraveljsonapi.io/docs/2.0/resources/relationships.html#specifying-data it should works as i expected ;)