TransWikia.com

I need a loop inside a model query

Stack Overflow Asked on February 5, 2021

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.

One Answer

    $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;
    }

Answered by yudhisthir nahar on February 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP