Pedro Pedro - 20 days ago 7
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('xml.gallery', compact('products'))->header('Content-Type', 'text/xml');

}


My blade (xml.gallery):

<?xml version="1.0"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>Test Store</title>
<link>http://domain.com</link>
<description>An example item from the feed</description>
@​foreach($products as $product)
<item>
<g:id>1</g:id>
<g:title>something</g:title>
<g:description>Solid plastic Dog Bowl in marine blue color</g:description>
<g:link>http://www.zara.com</g:link>
<g:image

_link>http://domain.com/images/photos_gallery/14788772681.png</g:image_link>
<g:price>12 GBP</g:price>
<g:brand>Nike</g:brand>
<g:availability>in stock</g:availability>
<g:condition>new</g:condition>
</item>
@​endforeach
</channel>
</rss>

Answer

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('xml.gallery', compact('products', 'xml_version'))->header('Content-Type', 'text/xml');

Which you can then place into your blade:

{{$xml_version}}
Comments