/**
  * 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();
         }
     }
 }
示例#2
0
function get_all_images_from_property_id($property_id)
{
    /* Find the junction table entries for pictures of this property */
    $photo_ids = PropertyPictureBridge::where('property_id', $property_id)->get();
    $images = array();
    /* Get all the photos of this property */
    if (count($photo_ids) < 1) {
        $placeholder = new Picture();
        $placeholder->url = env('PLACEHOLDER_IMAGE_URL');
        $placeholder->cloudinary_public_id = env('PLACEHOLDER_IMAGE_PUBLIC_ID');
        array_push($images, $placeholder);
    } else {
        foreach ($photo_ids as $photo_id) {
            array_push($images, Picture::find($photo_id->picture_id));
        }
    }
    return $images;
}
 public function searchPlaces(Request $request)
 {
     $data = $request->all();
     $properties = DB::table('properties')->where('owner_id', '!=', Auth::id());
     if ($request->has('city')) {
         $city = City::where('name', $data['city'])->first();
         $properties = $properties->where('city_id', '=', $city->id);
     }
     if ($request->has('district')) {
         $district = District::where('name', $data['district'])->first();
         $properties = $properties->where('district_id', '=', $district->id);
     }
     if ($request->has('type')) {
         $properties = $properties->where('type', '=', $data['type']);
     }
     if ($request->has('min_occupancy')) {
         $properties = $properties->where('max_occupancy', '>=', $data['min_occupancy']);
     }
     if ($request->has('max_occupancy')) {
         $properties = $properties->where('max_occupancy', '<=', $data['max_occupancy']);
     }
     if ($request->has('min_price_per_night')) {
         $properties = $properties->where('price_per_night', '>=', $data['min_price_per_night']);
     }
     if ($request->has('max_price_per_night')) {
         $properties = $properties->where('price_per_night', '<=', $data['max_price_per_night']);
     }
     $properties = $properties->join('property_features', 'properties.id', '=', 'property_features.property_id');
     if ($request->has('kitchen')) {
         $properties = $properties->where('kitchen', '=', on2true($data['kitchen']));
     }
     if ($request->has('internet')) {
         $properties = $properties->where('internet', '=', on2true($data['internet']));
     }
     if ($request->has('tv')) {
         $properties = $properties->where('tv', '=', on2true($data['tv']));
     }
     if ($request->has('essentials')) {
         $properties = $properties->where('essentials', '=', on2true($data['essentials']));
     }
     if ($request->has('shampoo')) {
         $properties = $properties->where('shampoo', '=', on2true($data['shampoo']));
     }
     if ($request->has('heating')) {
         $properties = $properties->where('heating', '=', on2true($data['heating']));
     }
     if ($request->has('air_conditioning')) {
         $properties = $properties->where('air_conditioning', '=', on2true($data['air_conditioning']));
     }
     if ($request->has('washer')) {
         $properties = $properties->where('washer', '=', on2true($data['washer']));
     }
     if ($request->has('dryer')) {
         $properties = $properties->where('dryer', '=', on2true($data['dryer']));
     }
     if ($request->has('free_parking_on_premises')) {
         $properties = $properties->where('free_parking_on_premises', '=', on2true($data['free_parking_on_premises']));
     }
     if ($request->has('wireless_internet')) {
         $properties = $properties->where('wireless_internet', '=', on2true($data['wireless_internet']));
     }
     if ($request->has('cable_tv')) {
         $properties = $properties->where('cable_tv', '=', on2true($data['cable_tv']));
     }
     if ($request->has('breakfast')) {
         $properties = $properties->where('breakfast', '=', on2true($data['breakfast']));
     }
     if ($request->has('pets_allowed')) {
         $properties = $properties->where('pets_allowed', '=', on2true($data['pets_allowed']));
     }
     if ($request->has('family_kid_friendly')) {
         $properties = $properties->where('family_kid_friendly', '=', on2true($data['family_kid_friendly']));
     }
     if ($request->has('suitable_for_events')) {
         $properties = $properties->where('suitable_for_events', '=', on2true($data['suitable_for_events']));
     }
     if ($request->has('smoking_allowed')) {
         $properties = $properties->where('smoking_allowed', '=', on2true($data['smoking_allowed']));
     }
     if ($request->has('wheelchair_accessible')) {
         $properties = $properties->where('wheelchair_accessible', '=', on2true($data['wheelchair_accessible']));
     }
     if ($request->has('elevator_in_building')) {
         $properties = $properties->where('elevator_in_building', '=', on2true($data['elevator_in_building']));
     }
     if ($request->has('indoor_fireplace')) {
         $properties = $properties->where('indoor_fireplace', '=', on2true($data['indoor_fireplace']));
     }
     if ($request->has('buzzer_wireless_intercom')) {
         $properties = $properties->where('buzzer_wireless_intercom', '=', on2true($data['buzzer_wireless_intercom']));
     }
     if ($request->has('doorman')) {
         $properties = $properties->where('doorman', '=', on2true($data['doorman']));
     }
     if ($request->has('pool')) {
         $properties = $properties->where('pool', '=', on2true($data['pool']));
     }
     if ($request->has('hot_tub')) {
         $properties = $properties->where('hot_tub', '=', on2true($data['hot_tub']));
     }
     if ($request->has('gym')) {
         $properties = $properties->where('gym', '=', on2true($data['gym']));
     }
     if ($request->has('feature_24_hour_check_in')) {
         $properties = $properties->where('feature_24_hour_check_in', '=', on2true($data['feature_24_hour_check_in']));
     }
     if ($request->has('hangers')) {
         $properties = $properties->where('hangers', '=', on2true($data['hangers']));
     }
     if ($request->has('iron')) {
         $properties = $properties->where('iron', '=', on2true($data['iron']));
     }
     if ($request->has('hair_dryer')) {
         $properties = $properties->where('hair_dryer', '=', on2true($data['hair_dryer']));
     }
     if ($request->has('laptop_friendly_workspace')) {
         $properties = $properties->where('laptop_friendly_workspace', '=', on2true($data['laptop_friendly_workspace']));
     }
     $properties = $properties->get();
     foreach ($properties as $property) {
         $property->id = $property->property_id;
         $property->city_name = City::find($property->city_id)->name;
         $photo_ids = PropertyPictureBridge::where('property_id', $property->id)->get();
         if (count($photo_ids) < 1) {
             $property->image_url = env('PLACEHOLDER_IMAGE_URL');
         } else {
             $property->image_url = Picture::find($photo_ids[0]->picture_id)->url;
         }
     }
     $cities = City::all();
     return redirect(url('/places/searchresult'))->with(['properties' => $properties, 'cities' => $cities]);
 }