/** * Execute the console command. * * @todo DON'T UPDATE LOCATIONS IF THEY'RE ALREADY SET * * @return mixed */ public function fire() { $Bodmin = Area::firstOrCreate(['name' => 'Bodmin']); // Ensure current locations are saved. foreach (array_unique(Interview::where('location_id', '=', '')->lists('location')) as $location) { if (strlen($location) === 0) { continue; // don't want to clobber real locations } $L = Location::firstOrNew(['name' => $location]); if ($L->exists) { continue; } $L->area()->associate($Bodmin); $L->save(); } Interview::all()->each(function ($Interview) { $Location = Location::where('name', '=', $Interview->location); if ($Location->count() <= 0) { \Log::error("This location not found despite having just been confirmed a few lines above", $Interview->toArray()); return; } $Interview->location_id = $Location->first()->id; $Interview->save(); }); }