public function run() { $imgs = Image::lists('id'); $tags = Tag::all(); foreach ($tags as $tag) { $tag->images()->attach($randValues = array_intersect_key($imgs, array_flip(array_rand($imgs, 2)))); } }
private function saveImages($images, $tag, $img_desc) { $this->deleteImages($tag->id); foreach ($images as $image) { $uniqueId = rand(1, 5000); $ext = $image->getClientOriginalExtension(); $images = Image::create(['slug' => $tag->slug . "." . $ext, 'img_name' => $image->getFilename() . '.' . $ext, 'body' => $img_desc, 'unique_id' => $uniqueId]); $tag->images()->attach($images); $file_info = pathinfo($image->getClientOriginalName()); \Image::make($image->getRealPath())->save(storage_path() . '/tag_images/' . $tag->slug . '-' . $uniqueId . '-' . $tag->id . '.' . $file_info['extension'])->destroy(); } }