function export(Request $request)
 {
     $regions = $request->get('regions');
     $daysCount = $request->get('day_count');
     $date = Carbon::createFromTimestamp(time() - $daysCount * 86400);
     $offers = Offer::query()->where('created_at', '>', $date)->get();
     echo '<ul>';
     /** @var \Illuminate\Bus\Dispatcher $dispatcher */
     $dispatcher = app('Illuminate\\Bus\\Dispatcher');
     foreach ($offers as $offer) {
         /** @var Offer $offer */
         if ($location = $offer->location and in_array($location->region, $regions, true)) {
             $dispatcher->dispatch((new \App\Jobs\ExportOffer($offer))->onQueue('export_offers'));
             echo "<li>created export job for <a href='/offer/'{$offer->id}'>{$offer->id}</a></li>";
         }
     }
     echo '</ul><p>done</p>';
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $this->enableInfo = $this->option('info');
     $cache = [];
     $bar = $this->enableInfo ? null : ($bar = $this->output->createProgressBar(Offer::count()));
     $offers = Offer::query()->chunk(100, function (Collection $offers) use($bar, &$cache) {
         $this->info("Got {$offers->count()} offers");
         foreach ($offers as $offer) {
             if ($bar) {
                 $bar->advance();
             }
             foreach ($offer->details as $key => $value) {
                 if (array_key_exists($key, $cache)) {
                     if (array_key_exists($value, $cache[$key])) {
                         $this->info("{$key}:{$value} exists in cache");
                         continue;
                     }
                 } else {
                     $cache[$key] = [];
                 }
                 $detailsParameter = DetailsParameter::firstOrCreate(['parameter' => $key]);
                 try {
                     $detailsValue = $detailsParameter->detailsValues()->firstOrCreate(['value' => $value]);
                     if ($detailsValue->wasRecentlyCreated) {
                         array_push($cache[$key], $value);
                         $this->info("Added {$key}:{$value}");
                     }
                 } catch (QueryException $e) {
                     if (23000 !== intval($e->getCode())) {
                         throw $e;
                     } else {
                         $this->info("Skipped {$key}:{$value}");
                     }
                 }
             }
         }
     });
 }
 function index()
 {
     return view('offers')->with(['offers' => Offer::query()->latest()->paginate('40')]);
 }