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

Rediska's avatar

How to display an XML file in a Laravel blade template?

I load an XML file and get the required element from it. Like that:

$xml = simplexml_load_file('example.com');
$$exampleElement = $xml->shop->offers->offer;

If I output it via {{dd($exampleElement)}} in the blade template, I get this:

SimpleXMLElement {#478 ▼
  +"@attributes": array:3 [▶
    "id" => "835376"
    "available" => "false"
  ]
  +"categoryId": "2411"
   ...
  +"oldprice": "2690"
  +"param": array:7 [▶
    0 => SimpleXMLElement {#1642 ▶
      +"@attributes": array:1 [▶
        "name" => "Color"
      ]
      +"0": "Green"
    }
    1 => SimpleXMLElement {#1643 ▶
      +"@attributes": array:1 [▶
        "name" => "Brand"
      ]
      +"0": "Adidas"
    }
    ...
  ]
  +"picture": array:2 [▶
    0 => "https://example.com/1.jpg"
    1 => "https://example.com/2.jpg"
  ]
}

If I output in the standard way via {{$exampleElement}}, then nothing is displayed.

What do I need to do so that the output of the element is in this format?:

<categoryId>2411</categoryId>
<param name="Color">Green</param>
...
0 likes
1 reply
Snapey's avatar
Snapey
Best Answer
Level 122

$exampleElement is an object so you cannot just echo it as if it were a string.

You can use {{ $exampleElement->asXML() }}

1 like

Please or to participate in this conversation.