Example: How to create dynamic sitemap with image tags

In this example the posts model has a belongsToMany relationship to images.

Route::get('sitemap', function(){

    // create new sitemap object
    $sitemap = App::make("sitemap");

    // set cache (key (string), duration in minutes (Carbon|Datetime|int), turn on/off (boolean))
    // by default cache is disabled
    $sitemap->setCache('laravel.sitemap', 3600);

    // check if there is cached sitemap and build new only if is not
    if (!$sitemap->isCached())
    {
         // add item to the sitemap (url, date, priority, freq)
         $sitemap->add(URL::to('/'), '2012-08-25T20:10:00+02:00', '1.0', 'daily');
         $sitemap->add(URL::to('page'), '2012-08-26T12:30:00+02:00', '0.9', 'monthly');

         // get all posts from db, with image relations
         $posts = DB::table('posts')->with('images')->orderBy('created_at', 'desc')->get();

         // add every post to the sitemap
         foreach ($posts as $post)
         {
            // get all images for the current post
            $images = array();
            foreach ($post->images as $image) {
                $images[] = array(
                    'url' => $image->url,
                    'title' => $image->title,
                    'caption' => $image->caption
                );
            }

            $sitemap->add($post->slug, $post->modified, $post->priority, $post->freq, $images);
         }
    }

    // show your sitemap (options: 'xml' (default), 'html', 'txt', 'ror-rss', 'ror-rdf')
    return $sitemap->render('xml');

});
Chapter 3 of 6