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

bobdebower's avatar

converting json to xml

Hey Guys how are you?

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?

0 likes
12 replies
MichalOravec's avatar

In controller

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
bobdebower's avatar

Thanks for you reply!

I tried this

 public function xml()
   {
    $data = Configuration::all(); 

    return response()->view('xml', compact('data'))->withHeaders([
        'Content-Type' => 'application/xml',
        'charset' => 'utf-8'
    ]);
}

in blade

 @php
 echo '<?xml version="1.0" encoding="UTF-8"?>';
@endphp

   @foreach ($data->json as $item)
  // add xml tags
  @endforeach

i got this error

Property [json] does not exist on this collection instance

what do you mean with adding xml tags? how?

thakyou for your time!

bobdebower's avatar

Thankyou for you message

i did this unfortunately i got this error: invalid argument supplied for foreach()

i have saved a json data in my database. $table->json('json') . i just want to loop and convert it as xml ;(

MichalOravec's avatar

@laradoel Under json column you don't have an array, so something like this.

@php
    echo '<?xml version="1.0" encoding="UTF-8"?>';
@endphp

<configurations>
    @foreach ($data as $configuration)
        <configuration>
           <something>{{ $configuration->json['something'] }}</something>

           <another>{{ $configuration->json['another'] }}</another>
        </configuration>
    @endforeach
</configurations>
bobdebower's avatar

I got this error

Illegal string offset 'something'

maybe i'am doing something wrong in my model.

Here is my model. i'am still using : $data = Configuration::all(); and your latest provided view

 class Configuration extends Model
  {
use HasFactory;
protected $fillable = [
    'mac_address','json'
];

 }
MichalOravec's avatar

So use your indexes from the json column, or what you have there...

bobdebower's avatar

yes i understand,

this what i get when i dd($data)

 #items: array:1 [▼
0 => App\Models\Configuration {#1232 ▼
  #fillable: array:2 [▶]
  #connection: "mysql"
  #table: "configurations"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:5 [▼
    "id" => 3
    "mac_address" => "1000"
    "json" => "{"id": 1, "name": "A green door", "tags": ["home", "green"], "price": 12.5, "checked": false, "dimensions": {"width": 5, "height": 10}}"
    "created_at" => "2020-10-28 15:25:12"
    "updated_at" => "2020-10-28 15:25:12"
  ]

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.

bobdebower's avatar

I'am alomost there thankyou sir.

i recieved this back:

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.

i use this

 <configurations>
@foreach ($data as $configuration)
    <configuration>
        <name>{{ $configuration->json['id'] }}</name>

    {{--            <another>{{ $configuration->json['another'] }}</another>--}}
        <configuration>
@endforeach
</configurations>

so if i want all the array data in the xml i have to write the loop dor every colmn? id, name, tags, price, etc?

MichalOravec's avatar
Level 75

Yes, you have to pass there everything what you want to have it in the xml.

@php
    echo '<?xml version="1.0" encoding="UTF-8"?>';
@endphp

<configurations>
    @foreach ($data as $configuration)
        <configuration>
           <id>{{ $configuration->json['id'] }}</id>

           <name>{{ $configuration->json['name'] }}</name>
        </configuration>
    @endforeach
</configurations>
1 like
bobdebower's avatar

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!

Please or to participate in this conversation.