title: How to export mysql table to xml file using laravel
body: <p>I am building a project with laravel 4. I am trying to export MySQL data to XML document. I have managed to save an .xml file however there is no data inside it, or when I add ->get() method to $sales the error pops up.</p>
  
  <p>How could I export selected data from the database to an xml file? I am aiming to create something like this: <a href="http://i.stack.imgur.com/lTIfp.png" rel="nofollow">image</a></p>
  
  <pre><code>$sales = Sale::where('Status', '=', 'ON')
          -&gt;orderBy('updated_at', 'asc'); //-&gt;get() here brings an error.
  
      try
      {
  
          $xml = new XMLWriter();
  
          $xml-&gt;openURI('documents/mailouts/example.xml');
  
          $xml-&gt;startDocument('1.0');
  
          $xml-&gt;startElement('sales');
  
          foreach ($sales as $sale) {
  
  
              $xml-&gt;startElement('sale');
  
              $xml-&gt;startElement('REF', $sale-&gt;SaleID);
              $xml-&gt;endElement();
  
              $xml-&gt;startElement('COUNTY', $sale-&gt;county);
              $xml-&gt;endElement();
  
              $xml-&gt;startElement('BOP1', $sale-&gt;SaleBop1);
              $xml-&gt;endElement();
  
              $xml-&gt;startElement('OFFICE', $sale-&gt;SaleOffice);
              $xml-&gt;endElement();
  
              $xml-&gt;endElement();
          }
  
          $xml-&gt;endElement();
          $xml-&gt;endDocument();
  
          $xml-&gt;flush();
  
          Session::flash('success', 'success.');
      }
      catch(Exception $e)
      {
          Session::flash('error','problem');
      }
  
  
      return Redirect::route('sendouts.generate');
  </code></pre>