【问题标题】:I need a loop inside a model query我需要一个模型查询中的循环
【发布时间】:2021-03-27 02:19:40
【问题描述】:

如何在模型中循环? 我有:

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

我需要某种方式来循环这个:->add(Url::create('https://mydomain/mycustompage/'))
我想像这样从我的数据库中获取信息:

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

我想要这样的东西:

$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'));  

我正在使用this package

【问题讨论】:

  • 这是一个很好的包,但它有问题。我必须实施 2 个额外的步骤才能使其正常工作。在一个拥有 130.000 条记录的网站上创建 dito 页面。它适用于测试,而不是产品(内存)。您可以从站点地图 xml 引用其他子地图。我遍历表格并逐行构建子图。阅读包页面上的问题。你会在那里找到一些灵感。包括已关闭的问题。
  • aditionaly 我可以使用这个包生成主站点地图,而不是制作我的自定义循环并将我的新数据添加到已经存在的站点地图:) 希望有更简单的方法可以做到这一点,没有它:)
  • 我想创建一个命令,这样我就可以每周两次将与作业相关联的事件排队。后者做艰苦的工作。我的问题是生产服务器内存不足。所以我不得不把它分块。没什么大不了的,但总的来说我花了 3 天时间完成任务。现在的奢侈是 laravel 可以自动完成,这要归功于队列管理器。

标签: php laravel eloquent xml-sitemap


【解决方案1】:
$products = 产品::all(); //要么 $products = Product::with('categories')->where('status', 1)->whereRaw('quantity is not null AND quantity > 0', 1)->whereNotNull('price'); //要么 $products = 产品::select([ '*', DB::raw('(案例 当 sale_price 不为空并且( now() = sale_from THEN sale_price 其他价格 END) product_price') ])->where('status', 1)->whereNotNull('price'); //现在for循环 foreach ($products as $key=>$product) { 回声$产品->名称; }

【讨论】:

  • sql没有问题,我需要我的sql结果在SitemamGenerator中以某种方式循环::
猜你喜欢
  • 2018-05-21
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
  • 2019-09-30
相关资源
最近更新 更多