Pedro Pedro - 1 year ago 330
PHP Question

Parsing XML in laravel

im trying to generate a xml but im getting a error:

FatalErrorException in 6955f07d83b93bb8aa89577b116866e228e0c155.php line 1 syntax error, unexpected 'version' (T_STRING).

I cant figure out what im doing wrong in my code.

My controller function is:

public function feed($gallery_id){
$products = Products::select("id","title","brand","url","path")->where("gallery_id",$gallery_id)->get();

foreach ($products as $product) {
$product->path = url($product->path);
return response()->view('', compact('products'))->header('Content-Type', 'text/xml');


My blade (

<?xml version="1.0"?>
<rss xmlns:g="" version="2.0">
<title>Test Store</title>
<description>An example item from the feed</description>
@‚Äčforeach($products as $product)
<g:description>Solid plastic Dog Bowl in marine blue color</g:description>

<g:price>12 GBP</g:price>
<g:availability>in stock</g:availability>

Answer Source

The way we've gotten around it is to store the xml header as a variable, then pass it along:

$xml_version = '<?xml version="1.0"?>';

return response()->view('', compact('products', 'xml_version'))->header('Content-Type', 'text/xml');

Which you can then place into your blade:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download