Reputation: 33
I get my data in the table I have a array stocked as a string I want when I get this data I don't get it with '""' , and the second probleme I get it with slaches ! like the fonctionnalites. this what I get
{
"id": 1,
"icon": "/lsapp/public/projects/icon/1558102023logoMazad.png",
"descriptions": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris ",
"fonctionnalites": "[\"CreativeDesign\",\"RetinaReady\",\"ModernDesign\",\"DigitalMarketingBranding\",\"rcveq\"]",
"screenshoot": "[\"/lsapp/public/projects/screenshoot/1558102023mazad1.png\",\"/lsapp/public/projects/screenshoot/1558102023mazad2.png\",\"/lsapp/public/projects/screenshoot/1558102023mazad3.png\",\"/lsapp/public/projects/screenshoot/1558102023mazad4.png\"]",
},
and I want get it like this
{
"id": 1,
"icon": "/lsapp/public/projects/icon/1558102023logoMazad.png",
"descriptions": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris ",
"fonctionnalites": ["CreativeDesign","RetinaReady","ModernDesign","DigitalMarketingBranding","rcveq"],
"screenshoot": ["/lsapp/public/projects/screenshoot/1558102023mazad1.png\","/lsapp/public/projects/screenshoot/1558102023mazad2.png","/lsapp/public/projects/screenshoot/1558102023mazad3.png","/lsapp/public/projects/screenshoot/1558102023mazad4.png"],
},
and this what I do to get my result
public function getproject(Request $request)
{
$services = WebProjects::all();
return response()->json($services);
}
Upvotes: 1
Views: 103
Reputation: 15257
It seems that your members fonctionnalites
and screenshoot
are already json encoded.
// in this example, the sub array "toto" is an encoded array
$arr = ["foo" => "bar", "toto" => json_encode(["forty", "two"])];
echo json_encode($arr); // outputs : {"foo":"bar","toto":"[\"forty\",\"two\"]"}
You can decode it first, before re-encoding the whole array/object
$arr = ["foo" => "bar", "toto" => json_encode(["forty", "two"])];
echo json_encode($arr); //{"foo":"bar","toto":"[\"forty\",\"two\"]"}
$arr["toto"] = json_decode($arr["toto"]);
echo json_encode($arr); //{"foo":"bar","toto":["forty","two"]}
Upvotes: 1
Reputation: 2398
Try this:
return response()->json($services, 200, [], JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
if you have non-encoded string data in the database.
Reference to json flag and how flags are used in Laravel.
By the way, Laravel understands when Collection is returned as a response and serializes it, you can go just with return $services;
.
P. S. Take a look on Cid's answer, if your data is already encoded and stored as string in the database.
Upvotes: 1