Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Minotti's avatar

Backslashes on URL with API Resources

Hello everyone.

I have a problem using Resource in Laravel. When any string contains a single slash "/" it is returned in the API with two slashes, a normal slash and a backslashe.

Like this: "http: \ / \ / domain.com \ / storage \ / imagens \ / ......."

Or this: 1993 \ / 28 \ / 04

PS.: I inserted spaces between the slashes and words to appear correctly here in the post.

I do not know if this would imply the functioning of those who will consume the API, however I find it very uncomfortable. And when copying this link and putting it in the browser it would be "storage//images//" ..

I'm using the Resource's withResponse and it still doesn't work.

See my code:

Images resource: public function withResponse ($request, $response) { $response->setEncodingOptions(JSON_UNESCAPED_SLASHES); }

Controller: ... return response (new UserResource($user));

Inside UserResource I basically call the ImageResource::collection ($this->images);

What am I doing wrong? Or is this really the expected behavior?

0 likes
2 replies
Bcrypt's avatar

Try returning the Resource alone without the response function:

return new UserResource($user) I think that should do it.

Please or to participate in this conversation.