I'am saving json in my database this way: $table->json('json');
i made an api with all the get/post/delte. put request, works fine! but i want to be able to convert the saved json data to Xml. so i save it in the database, i want to convert it now to xml now. does anyone have tips, how to do this?
public function xml()
{
$data = Model::first(); // just an example
return response()->view('xml', compact('data'))->withHeaders([
'Content-Type' => 'application/xml',
'charset' => 'utf-8'
]);
}
In view xml.blade.php
@php
echo '<?xml version="1.0" encoding="UTF-8"?>';
@endphp
@foreach ($data->json as $item)
// add xml tags
@endforeach
but what indexex do i use for the colmn? i tried id or name or json. i recieved the same error : Illegal string offset 'something' ;(. i just want to convert all the whole json array.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Configuration extends Model
{
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'json' => 'array',
];
}
This page contains the following errors:
error on line 8 at column 35: Opening and ending tag mismatch: configuration line 0 and configurations
Below is a rendering of the page up to the first error.
i recieved back the id: '1' but it still not in a xml tag.
Thakyou sir this worked perfect. i was wondering, because i saved the json as a array, is it possible just covert the whole array json to xml . or do i have to write a different code for? Thanyou sir for helpin!