/**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     if (rand(0, 1) == 1) {
         $url = "https://pixabay.com/api/?key=" . env('PIXABAY_API_KEY') . "&image_type=photo&category=buildings&safesearch=true&min_width=500";
         $json = file_get_contents($url);
         $json_data = json_decode($json, true);
         $pixa_json = $json_data['hits'];
         foreach ($pixa_json as $image) {
             $pictureUploadResult = \Cloudinary\Uploader::upload($image['webformatURL']);
             if ($pictureUploadResult == null) {
                 echo "Error uploading picture";
                 continue;
             }
             $picture = new Picture();
             $picture->description = "";
             $picture->url = $pictureUploadResult['secure_url'];
             $picture->cloudinary_public_id = $pictureUploadResult['public_id'];
             $picture->save();
             $junction = new PropertyPictureBridge();
             $all_properties = Property::all();
             $one_property = $all_properties[rand(0, count($all_properties) - 1)];
             $junction->property_id = $one_property->id;
             $junction->picture_id = $picture->id;
             $junction->save();
         }
     } else {
         $api_key = env('FLICKR_API_KEY');
         $query = 'new york city apartment';
         $url = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=" . $api_key . "&tags=" . urlencode($query) . "&safe_search=1&per_page=20&format=json";
         $json_data = file_get_contents($url);
         $json_data = substr($json_data, 14);
         $json_data = substr($json_data, 0, strlen($json_data) - 1);
         $json_data = json_decode($json_data, true);
         foreach ($json_data['photos']['photo'] as $photo) {
             $farm = $photo['farm'];
             $server = $photo['server'];
             $src = 'http://farm' . $farm . '.static.flickr.com/' . $server . '/' . $photo['id'] . '_' . $photo['secret'] . '_z.jpg';
             $pictureUploadResult = \Cloudinary\Uploader::upload($src);
             if ($pictureUploadResult == null) {
                 echo "Error uploading picture";
                 continue;
             }
             $picture = new Picture();
             $picture->description = "";
             $picture->url = $pictureUploadResult['secure_url'];
             $picture->cloudinary_public_id = $pictureUploadResult['public_id'];
             $picture->save();
             $junction = new PropertyPictureBridge();
             $all_properties = Property::all();
             $one_property = $all_properties[rand(0, count($all_properties) - 1)];
             $junction->property_id = $one_property->id;
             $junction->picture_id = $picture->id;
             $junction->save();
         }
     }
 }
Beispiel #2
0
                $degree->primary_degree = true;
            } else {
                $degree->primary_degree = false;
            }
            $degree->save();
        }
        return redirect(url('profile'));
    }));
    Route::post('/property/add/photo', function (Request $request) {
        $validator = Validator::make($request->all(), ['property_id' => 'required|numeric|min:0|integer', 'image' => 'required|image']);
        if ($validator->fails()) {
            return redirect(url('/property/add/photo/' . $request->property_id))->withInput()->withErrors($validator);
        }
        $pictureUploadResult = \Cloudinary\Uploader::upload($request->image);
        if ($pictureUploadResult == null) {
            $errorArr = array("The image upload service is down, sorry!");
            return redirect(url('/property/add/photo/' . $request->property_id))->withInput()->withErrors($errorArr);
        }
        $picture = new Picture();
        // TODO: Get an (optional) description from users?
        $picture->description = "";
        $picture->url = $pictureUploadResult['secure_url'];
        $picture->cloudinary_public_id = $pictureUploadResult['public_id'];
        $picture->save();
        $junction = new PropertyPictureBridge();
        $junction->property_id = $request->property_id;
        $junction->picture_id = $picture->id;
        $junction->save();
        return redirect(url('/property/edit/photos/' . $request->property_id));
    });
});