Skip to content

Laravel 10, the toArray of the resource collection requires an instance of the Illuminate\Http\Request class #46226

Answered by fh32000
fh32000 asked this question in Q&A
Discussion options

You must be logged in to vote

For those who might be facing a similar issue, the problem was related to the fact that in Laravel 10, the toArray method in Eloquent resources no longer accepts an integer parameter like in Laravel 9. Instead, you need to pass a request object to it.

One possible solution suggested in Laravel Discord by @ Tippin was to use the resolve method to get the array representation of the resource without having to pass a request object to the toArray method. Here's an example:

$resource = UserResource::make($user)->resolve();

This approach should work in both Laravel 9 and Laravel 10.

https://github.com/laravel/framework/blob/10.x/src/Illuminate/Http/Resources/Json/JsonResource.php#L105-L118

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by fh32000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant