Laravel数据库通知存储URL自动添加转义斜杠问题求助
Hey there, let's break down what's happening here and put your mind at ease—this isn't a MySQL injection protection quirk, it's just how JSON serialization works in Laravel.
The Root Cause
When Laravel saves your notification's data array to the database, it automatically converts that array into a JSON string. In JSON syntax, forward slashes (/) are escaped as \/ by default. That's why you see {"msg":"https:\/\/google.com"} in your database—it's just the raw JSON representation of your URL.
Here's the Good News: It's Harmless!
When you pull the notification data back out of the database (like $notification->data['msg']), Laravel will automatically turn that JSON string back into a regular PHP string. The escaped slashes will vanish, and you'll get your original URL https://google.com exactly as you intended.
Test it out with a quick snippet:
$notification = Notification::find(your_notification_id); echo $notification->data['msg']; // This will output: https://google.com
Quick Fix for Your Commented Code (Bonus Tip)
I spotted a small mistake in your commented msg line—you're wrapping $this->post->slug in single quotes inside the route() call, which means PHP will treat it as a literal string instead of the actual slug value. Fix it like this:
// Replace this: // route('$this->post->slug') // With either (if your route name matches the slug): route($this->post->slug); // Or (if using a named route with a parameter): route('posts.view', ['slug' => $this->post->slug]);
Rendering in Views
If you're worried about displaying this URL in a Blade view, don't be—Blade will handle it automatically. For example:
<a href="{{ $notification->data['msg'] }}">View Post</a> <!-- This will render as a normal link with https://google.com as the href -->
The escaped slashes are only present in the raw database storage—they won't affect how you use the URL anywhere in your application.
内容的提问来源于stack exchange,提问作者Leong

