public function run() { $colors = ['Galvalume' => ['hex' => 'cad0d2', 'image' => 'galvalume.png'], 'Pure White' => ['hex' => 'eceaee'], 'Polar White' => ['hex' => 'f0f2f3'], 'Ivory' => ['hex' => 'f8e5c6'], 'Light Stone' => ['hex' => 'cccab2'], 'Mocha Tan' => ['hex' => 'b1946f'], 'Patina Green' => ['hex' => '6c7f56'], 'Marine Green' => ['hex' => '5b9994'], 'Forest Green' => ['hex' => '10411e'], 'Hawaiian Blue' => ['hex' => '426d7e'], 'Gallery Blue' => ['hex' => '073e60'], 'Barn Red' => ['hex' => '6e1f19'], 'Patriot Red' => ['hex' => '8c181b'], 'Burgundy' => ['hex' => '512127'], 'Cocoa Brown' => ['hex' => '513d30'], 'Metallic Copper' => ['hex' => 'b15726'], 'Galvanized Silver' => ['hex' => 'afafb1', 'image' => 'galvanized_silver.png'], 'Old Town Gray' => ['hex' => '898b8e'], 'Clay' => ['hex' => '979888'], 'Charcoal Gray' => ['hex' => '565656'], 'Burnished Slate' => ['hex' => '342019'], 'Black' => ['hex' => '030303'], 'Ash Gray' => ['hex' => 'aca1a1'], 'Evergreen' => ['hex' => '394f46']]; $dirname = dirname(__FILE__); foreach ($colors as $colorName => $colorInfo) { if (Color::where('name', '=', $colorName)->first()) { continue; } if (empty($colorInfo['hex'])) { // Nothing we can do (yet) continue; } // Upload image, if exists $colorImage = null; if (isset($colorInfo['image'])) { copy($dirname . "/default_images/colors/" . $colorInfo['image'], $dirname . "/" . $colorInfo['image']); $file = new UploadedFile($dirname . "/" . $colorInfo['image'], $colorInfo['image'], "image/png", filesize($dirname . "/" . $colorInfo['image']), null, true); $colorImage = ImageList::upload($file); } $redHex = substr($colorInfo['hex'], 0, 2); $greenHex = substr($colorInfo['hex'], 2, 2); $blueHex = substr($colorInfo['hex'], 4, 2); $color = Color::firstOrCreate(['name' => $colorName, 'red' => hexdec($redHex), 'green' => hexdec($greenHex), 'blue' => hexdec($blueHex)]); $color->save(); if ($colorImage != null) { $color->image()->save($colorImage); } foreach (Location::all() as $location) { foreach (Gauge::all() as $gauge) { $locationColorGauge = new LocationColorGauge(); $locationColorGauge->location()->associate($location); $locationColorGauge->color()->associate($color); $locationColorGauge->gauge()->associate($gauge); $locationColorGauge->save(); } } } }
public function attach() { $location = Location::find(Request::input('location')); $color = Color::find(Request::input('color')); foreach (Gauge::all() as $gauge) { $locationColorGauge = new LocationColorGauge(); $locationColorGauge->location()->associate($location); $locationColorGauge->color()->associate($color); $locationColorGauge->gauge()->associate($gauge); $locationColorGauge->save(); } return "success"; }