Is the $request Parameter Needed JsonResource->toArray() Method #54395
Unanswered
rdehnhardt
asked this question in
Ideas
Replies: 2 comments 3 replies
-
It was introduced in laravel 10 and the child uses the request param. It is overwritten in \Illuminate\Http\Resources\Json\ResourceCollection::toArray |
Beta Was this translation helpful? Give feedback.
3 replies
-
I solved it using the resolve() method; it ended up like this:
MessageResource::make(...)->resolve(). However, I’m not sure if it’s the
best option because internally, resolve() creates a request instance, which
isn’t necessary at this point.
…On Wed 29. Jan 2025 at 16:47, bulletproof-coding ***@***.***> wrote:
To me it seems like a leftover. I can't find any function toArray that has
Request as param other that these 2 from JsonResource and ResourceCollection
—
Reply to this email directly, view it on GitHub
<#54395 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABV74VOQKAQHAMPT7V6M3ED2NDZ25AVCNFSM6AAAAABWCP6UACVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCOJZG4YTKOA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello! I’m working on an event that sends a message to a broadcast channel. To keep things consistent, I wanted to reuse the same MessageResource I use in my controller to return data to the frontend, as it’s already configured correctly.
When trying to pass the MessageResource to my event (not sure if this is the standard approach), I need to convert it to an array for the broadcastWith() method. However, I noticed that calling MessageResource::make($this->message)->toArray() requires a $request parameter. For example:
Upon inspecting Laravel’s JsonResource class, the toArray() method includes a $request parameter, but it doesn’t seem to be used internally. Here’s the code snippet from Illuminate\Http\Resources\Json\JsonResource:
My question: Is the $request parameter actually necessary here, especially when using resources for broadcasting? If not, why is it required in the method signature?
Thanks in advance for any insights! 😊
Beta Was this translation helpful? Give feedback.
All reactions