1

How can I loop in a model? I have:

SitemapGenerator::create(config('app.url'))
->configureCrawler(function (Crawler $crawler) {
       $crawler->setMaximumDepth(4);
  })
->add(Url::create('https://mydomain/mycustompage/'))
->getSitemap()
->writeToFile(public_path('sitemap.xml'));     

I need someway to loop this: ->add(Url::create('https://mydomain/mycustompage/'))
I want to get info from my DB like this:

$all_active_products =  DB::table('products')->select('slug')->where('is_active',1)->whereNull('deleted_at')->get();

And I want something like this:

$all_active_products =  DB::table('products')->select('slug')->where('is_active',1)->whereNull('deleted_at')->get();
SitemapGenerator::create(config('app.url'))
    ->configureCrawler(function (Crawler $crawler) {
        $crawler->setMaximumDepth(4);
   })
   foreach ($all_active_products as $a){
      ->add(Url::create('https://mydomain/mycustompage/'.$a->slug))
   }

   ->getSitemap()
   ->writeToFile(public_path('sitemap.xml'));  

I am using this package.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
Alexey
  • 13
  • 1
  • 3
  • It's a good package but it has issues. I had to implement 2 additional steps to make it work. On a website with 130.000 records creating dito pages. It worked on test, not on prod (memory). You can refer from a sitemap xml to other submaps. I looped through the tables and build submaps line by line. Read the issues on the package page. You'll find some inspiration there. Including the closed issues. – Dimitri Mostrey Dec 16 '20 at 12:24
  • aditionaly i can generate main sitemap with this package than make my custom loop and add my new data to already existing sitemap :) hope there is more simplest way to do that, without it :) – Alexey Dec 16 '20 at 12:34
  • I wanted to create a command so I could queue it twice a week with an event linked to a job. The latter does the hard work. My problem was that the production server ran out of memory. So I had to chunk it. Not a big deal, but all in all it took me 3 days to complete the task. The luxury now is that laravel does it automatically thanks to the queue manager. – Dimitri Mostrey Dec 16 '20 at 12:42

1 Answers1

-1
    $products = Product::all();

    //or

    $products = Product::with('categories')->where('status', 1)->whereRaw('quantity is not null AND quantity > 0 ', 1)->whereNotNull('price');

    //or

    $products = Product::select([
        '*',
        DB::raw('(CASE
                    WHEN sale_price is not null AND ( now() = sale_from THEN sale_price
                    ELSE price
                END) product_price')
    ])->where('status', 1)->whereNotNull('price');

    //now for loop

    foreach ($products as $key=>$product) {
        echo $product->name;
    }