2

Instead of this "{\"name\":\"Shares Magazine\"}" I want this {"name":"Shares Magazine"}.

To achieve this, I am doing this json_encode(["name" => $user->reference_source], JSON_UNESCAPED_SLASHES);

To no avail, as this always ends up in the database "{\"name\":\"Shares Magazine\"}".

2
  • Does this answer your question? how to remove back slashes from json output in php Commented Mar 9, 2020 at 6:55
  • Doesn't work as it still appears as "{\"name\":\"Press\"}" and I want it to appear as it says in OP Commented Mar 9, 2020 at 6:57

2 Answers 2

1

I'm assuming you're using a json type column in your database

Schema::create('articles', function (Blueprint $table) {
    $table->id();
    $table->json('json_column');
});

And you're also casting in your model

class Article extends Model
{
    protected $casts = [
        'json_column' => 'json',
    ];
}

Then you don't need to cast before saving. Otherwise you're ending with a double cast. Laravel does that for you.

Article::create([
    'json_column' => ['name' => 'Shares Magazine'],
]);

And it be will saved as {"name": "Shares Magazine"}.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you I actually spotted it too
0

Try this after encoding your JSON (this works better than JSON_UNESCAPED_SLASHES):

$json = preg_replace('/\\\"/',"\"", $json);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.