generating sitemap as a Command, throwing "type error"
Created by: dingman
I put this into a command so I can run it on-demand as I need to, probably once per week.
I'm getting the following error for my Product model. Any help?
Using latest 5.5 and latest version of the package.
Type error: Argument 1 passed to Illuminate\Database\Eloquent\Builder::make() must be of the type array, string given, called in /home/vagrant/code/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 1470
My Command
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class GenerateSitemaps extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'generate:sitemap';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// create new sitemap object
$sitemap = \App\Product::make('sitemap');
// get all products from db (or wherever you store them)
$products = \App\Product::with('details')->orderBy('id','asc')->get();
// counters
$counter = 0;
$sitemapCounter = 0;
// add every product to multiple sitemaps with one sitemap index
foreach ($products as $p) {
if ($counter == 50000) {
// generate new sitemap file
$sitemap->store('xml', 'sitemap-' . $sitemapCounter);
// add the file to the sitemaps array
$sitemap->addSitemap(secure_url('sitemap-' . $sitemapCounter . '.xml'));
// reset items array (clear memory)
$sitemap->model->resetItems();
// reset the counter
$counter = 0;
// count generated sitemap
$sitemapCounter++;
}
// add product to items array
$p->freq = 'daily';
$p->priority = '0.9';
$sitemap->add(route('product', $p->slug), $p->details->updated_at, $p->priority, $p->freq);
// count number of elements
$counter++;
}
// you need to check for unused items
if (!empty($sitemap->model->getItems())) {
// generate sitemap with last items
$sitemap->store('xml', 'sitemap-' . $sitemapCounter);
// add sitemap to sitemaps array
$sitemap->addSitemap(secure_url('sitemap-' . $sitemapCounter . '.xml'));
// reset items array
$sitemap->model->resetItems();
}
// generate new sitemapindex that will contain all generated sitemaps above
$sitemap->store('sitemapindex', 'sitemap');
}
}