/** * Query and return blog posts * * @return Illuminate\Database\Eloquent\Collection */ public function onRun() { // Start building the tags query $query = ProductsTag::with('products'); // Hide orphans if ($this->property('hideOrphans')) { $query->has('products', '>', 0); } // Sort the tags $subQuery = DB::raw('( select count(*) from `abnmt_products_products_tags` where `abnmt_products_products_tags`.`tag_id` = `abnmt_products_tags`.`id` )'); $key = $this->property('orderBy') ?: $subQuery; $query->orderBy($key, $this->property('direction')); // Limit the number of results if ($take = intval($this->property('results'))) { $query->take($take); } $this->tags = $query->get(); }