-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
Breaking change in ResourceCollection #32513
Comments
Now that I see it like this, I'm not so sure this is a bug. We don't document transforming the dataset like this anywhere. |
Maybe we should add extra guards in the code to help with developer experience, giving good exceptions? |
@driesvints We know it's not the standard procedure, but sometimes people have to customize the payload a bit. Where a |
What are the values of the collection? How are you instantiating them? |
I will explain with an example. Say you have a But now that can't be done. #32296 will break a lot of these kind of applications. |
What's preventing you from doing it the way that it's documented? <?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class Post extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
];
}
} use App\Http\Resources\ Post as PostResource;
use App\Post;
Route::get('/posts', function () {
return PostResource::collection(Post::all());
}); |
I use it like that : public function __invoke(Request $request): IndexCollection
{
// some logic
return new IndexCollection($pages);
} I prefere return a |
Well, typically the collection uses the singular resource for the collection:
I see your use case but I don't think we ever really supported that. |
Because some people (like me) keep I get that it's not the ideal way, but I was just explaining why people might want to use this kind of technique. It's pretty obvious. I agree with @GrahamCampbell, that there should be some kind of exception, if something is not supported. Like @Shhu, it took me 2 whole days to figure out why that particular code was not working, only to find out later that a pull request was behind the errors. |
This one took me a little while to figure out as well. I can't say for sure where I saw an example for the array style transform:
But it did offer some benefits over the way transform is shown in the official documentation. Mainly, changing model attributes directly caused issues with some of my model mutators (eg reformatting dates to be strings, adding '$' to currency fields, etc). I wound up re-coding the transformers but I had to add extra attributes to my models that don't exist in the schema (which felt a little hacky). |
Fixed in 7.8.0. |
Was getting this error on Laravel v6.18. I guess I was doing the transformation based on this StackOverflow answer. I don't have enough reputation to comment a warning about it, but if anyone can, please do. |
From the OP:
This article only ever They even say that in the article:
|
@taylorotwell can you do the same for 6.x branch please? |
Description:
cf #32296 (comment)
$this->collection->transform()
throw an errorTrying to get property 'resource' of non-object'
Works in 7.5.2, break in 7.6.0 and above
Have to replace
$this->collection->transform(
by$this->collectResource($this->collection)->transform(
Steps To Reproduce:
Usage of
$this->collection->transform
based on https://medium.com/@dinotedesco/laravel-api-resources-what-if-you-want-to-manipulate-your-models-before-transformation-8982846ad22cThe text was updated successfully, but these errors were encountered: