title: how to render a view and save the content in a file in Laravel
body: <p>I'm having a hard time trying something very trivial in my opinion.</p>
  
  <p>I want to make a view, get the string contents and save it to an xml file</p>
  
  <p>Here is an example code of what i try, but it outputs the xml view to the browser</p>
  
  <pre><code>use Illuminate\Filesystem\Filesystem;
  
  class ExportController extends BaseController {
  
      function getIndex(){
          $fs = new Filesystem();
  
          $data = array();
          $view = View::make('xml', $data);
  
          $html = $view-&gt;render(); //outputs content to the browser
  
          $fs-&gt;put("exercise.xml", $html);
      }
  }
  </code></pre>
  
  <p>I can't find any other method in the View API to get the contents, __toString also calls the render function internally. Am I missing something here?</p>
  
  <p><strong>EDIT</strong>: this is my full export controller: i want to export exercises from a database to standalone html and XML files. The hickup is at the bottom where I loop the exercises. The view gets outputted to the browser and the foreach stops</p>
  
  <p>
  
  <pre><code>use Illuminate\Filesystem\Filesystem;
  
  set_time_limit(0);
  
  class ExportController extends BaseController {
  
      function __construct(){
  
      }
  
      function getIndex($methodId = NULL, $exerciseId = NULL){
  
          $base = base_path() . "/export/";
          $playerPath = base_path() . "/public/preview/dist/";
          $uploadsPath = base_path() . "/public/uploads/";
  
          if($methodId ===NULL &amp;&amp; $exerciseId === NULL){
              $up = new Exception('Please provide a method-id or an exercise id');
              throw $up;
          }
  
          //we make an array which holds all the exercises we have to export
          $exercises = array();
  
          //get all exercises for given methodId
          if($methodId !== NULL){
              $method = Method::with('categories.exercises')-&gt;find($methodId);
              if($method == NULL) break;
  
              foreach($method-&gt;categories as $category){
                  foreach($category-&gt;exercises as $exercise){
                      array_push($exercises, $exercise);
                  }
              }
          }
  
  
          if($exerciseId != null){
              $exercise = Exercise::find($exerciseId);
              if($exercise != NULL) array_push($exercises, $exercise);
          }
  
          if(empty($exercises)){
              $up = new Exception('No exercises could be found for given method/exercise');
              throw $up;
          }
  
          //loop the exercises and publish like a charm
          foreach($exercises as $exercise){
              //determine destination
              $path = $base . $exercise-&gt;getPath();
  
              //check if path exists, if it does, wipe it out
              $fs = new Filesystem();
              if($fs-&gt;exists($path)){
                  $fs-&gt;deleteDirectory($path, true);
              }
  
              //copy player files
              echo "copying " . $path . "&lt;br /&gt;";
              $fs-&gt;copyDirectory($playerPath, $path);
  
              //copy resources
              echo "copying resources " . $path . "&lt;br /&gt;";
              $fs-&gt;copyDirectory($uploadsPath . $exercise-&gt;id . "/", $path);
  
  
              //save xml file
              $content = Exercise::getXmlContent($exercise-&gt;id);
              $fs-&gt;put($path . "exercise.xml", View::make('xml', $content));
          }
      }
  
  }
  </code></pre>