-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem with countables relationships #271
Comments
Do you have a |
Thanks @lindyhopchris for the fast reply, I dont use any **CollectionQuery at all, the only thing related may be the relationship method on the Container model
but If I remove the custom sorting the results is the same without any "meta". |
Does it work if you remove the Basically that isn't a typical Eloquent relationship there - so I'd suspect something to do with that is a problem. Does it work if you just change it to: public function rows()
{
return $this->hasMany(Row::class, 'container_id', 'id');
} Which is a more typical Eloquent relationship? FYI I wouldn't recommend doing all those things on the relationship in the |
If I use this typical relation:
I still dont get any "meta" field on the response:
this is the full response btw {
"jsonapi": {
"version": "1.0"
},
"links": {
"self": "https://domain.com/v1/containers/115"
},
"data": {
"type": "containers",
"id": "115",
"attributes": {
"label": "prova",
"weight": 3,
"livemode": false,
"container_id": null,
"image_path": "assets/containers/115/y5f3IBxosTZbzCPuUQo7CP9r8UDR1NKlswOQKqNb.jpg",
"image_url": "https://domain.s3.eu-central-1.amazonaws.com/assets/containers/115/y5f3IBxosTZbzCPuUQo7CP9r8UDR1NKlswOQKqNb.jpg?...blablabla",
"description": null,
"attributes": null,
"created_at": "2024-01-22T15:57:01.000000Z",
"updated_at": "2024-02-11T17:45:51.000000Z"
},
"relationships": {
"rows": {
"links": {
"related": "https://domain.com/v1/containers/115/rows"
}
},
"products": {
"links": {
"related": "https://domain.com/v1/containers/115/products"
}
},
"containers": {
"links": {
"related": "https://domain.com/v1/containers/115/containers"
}
}
},
"links": {
"self": "https://domain.com/v1/containers/115"
}
}
}
|
So this is working in the automated tests, so I'm going to need you to debug as it's impossible for me to say based on this information. Here's an automated test for it which is why I know it works: laravel/tests/dummy/tests/Api/V1/Posts/ReadTest.php Lines 212 to 231 in 567263e
Some things I'd need to know: Do you have a custom controller action? If you don't have a custom controller action, can you debug the model here:
To see whether it has the count information in it? I believe Eloquent would call it |
I was looking at the custom controller, I have a custom controller for Containers:
I checked with another resource that hasnot a custom controller but I still dont get the "meta" |
So the It would help if you can debug the model returned here:
In its attributes, does it have a |
Just to clarify something - I'm not saying there isn't a bug in the package. But when I have a passing automated test for exactly this scenario, and I don't have access to your application, then I need you to provide more debug insight, because otherwise I'm just totally guessing as to what the problem might be! |
Ok, I got it, yes there is a
Maybe the problem is on the resource? I use **Resource.php on every model |
Thanks so much for debugging this - it's great to confirm that the value is in the model as that means this package is loading the data properly. My next questions was going to be: are you using a resource class? Which you've already answered! If you're using your own resource class, then you'd need to assign the meta to the relationship yourself: Out of interest, why are you using resource classes? Generally it's not required. |
Well done! Im glad we found out what's the problem. We use resources cause we need to control what will be exposed to an external client by the api. Now I have a question,
Ex. This will always include the meta for the relation, how do I include the meta only when requested by the client? Something like this but seems a bit "procedural"
|
Yeah you have to manage the meta field by yourself as you have chosen to manage the resource by yourself. If you want the package to manage the meta field you should not use a resource class. As you've implemented a specific resource class, you shouldn't do anything "generic" like you're proposing, because it would be inefficient. In your resource class, you know that the row count might be there, so all you need to do is: $this->relation('rows')->withMeta(fn () => ['count' => $this->rows_count]); You can obviously put logic in there if you only want the count to show if As a side note, I really wouldn't recommend using I'm probably going to have to start deprecating and removing some of the usages of the resource class as it's enabling people to deviate from the spec. Which is bad, because the whole point of the package is to implement the spec. |
You might find it's better not to bother with the resource class, and instead use this feature: I think if you use that (i.e. remove the resource class and instead rely on serialisation customisation in the schema class) then the count meta should automatically be added. You'd need to check that though. |
$this->relation('rows')->withoutSelfLink()->withMeta(fn () => is_numeric($this->rows_count) ? ['rows_count' => $this->rows_count] : null), one line code Thank you very much for the advice you are giving me. I will ask the developers to do an in-depth analysis in order to simplify the code. For the sake of "withoutSelfLink", from a frontend point of view, it is useless. With the frontend we must have the resource available and avoid as many calls to the API as possible. I decided not to show it: 1. to simplify the response json, 2. not to give fe/nders a chance to cause confusion. For the point about resources, I find it is cleaner to have the code well structured and divided rather than having to manage everything on the schema but this is a stylistic preference more than anything else. The important thing is to be consistent in our decisions. Thanks for what you do and keep it up! |
Sure no problem and thanks for the explanation. I'll leave this ticket open as I need to add something to the docs on countable relationships, making it clear you have to add the meta manually if you have a resource class. |
Hi,
I'm following the docs https://laraveljsonapi.io/docs/3.0/digging-deeper/countable.html about countable relationships but seems to not having any results from it.
I have a complete set of json-api working in production so Im willing Im doing things right.
Now for a new FE I need to know before if a relation "exists", has more than 0 models. I found that countable relationships can do the trick so:
I have a ContainerSchema:
Now on the request side I can do this:
and should see the meta attribute within the rows relationship. But the response doesn't change at all, same response as before adding the
->canCount()
method.Im using :
"laravel-json-api/laravel": "^3.2"
any suggestions?The text was updated successfully, but these errors were encountered: