每 15 分钟将此 URL 中的 json 数据保存并更新到 laravel 数据库项目中



我想将这些值保存到数据库中。 目前,它仅保存 1 条记录,即第一条记录。但是,我希望它保存每一个。这对我来说很复杂,因为有一些数组和对象。

这是我从链接创建/更新数据的代码: 任何帮助将不胜感激,我得到了错误异常:数组到字符串的转换。

<?php
$responses= file_get_contents('https://app.pyber.nl/export/objects/642e92efb79421734881b53e1e1b18b6');
$responses = json_decode($responses);
foreach($responses as $response){
$object = Objects::create([
'agency_id' => $agency->id,
'user_id' => $agency->user_id,
'category' => $response->category,
'category_rename' => $response->category_rename,
'neighborhood' => $response->neighborhood,
'archived' => $response->archived,
'type' => $response->type,
'subtype' => $response->subtype,
'build_year' => $response->build_year,
'build_period' => $response->build_period,
'living_space' => $response->living_space,
'plot_surface' => $response->plot_surface,
'volume' => $response->volume,
'rooms' => $response->rooms,
'bedrooms' => $response->bedrooms,
'bathrooms' => $response->bathrooms,
'status' => $response->status,
'service_costs' => $response->service_costs,
'new_construction' => $response->new_construction,
'holiday_rental' => $response->holiday_rental,
'pets_allowed' => $response->pets_allowed,
'gated_community' => $response->gated_community,
'featureFacilities' => json_encode($response->featureFacilities),
'locations' => json_encode($response->locations),
'views' => json_encode($response->views),
'land_registries' => json_encode($response->land_registries),
'open_house_dates' => json_encode($response->open_house_dates),
'acceptance' => $response->acceptance,
'price' => $response->price,
'condition' => $response->condition,
'address' => $response->address,
'street_address' => $response->street_address,
'zip' => $response->zip,
'city' => $response->city,
'country' => $response->country,
'latitude' => $response->latitude,
'longitude' => $response->longitude,
'open_house' => $response->open_house,
'investment' => $response->investment,
'registration_type' => $response->registration_type,
'sale' => $response->sale,
'rent' => $response->rent,
'description_nl' => $response->description_nl,
'description_en' => $response->description_en,
'living_styles' => json_encode($response->living_styles),
'image' => $response->image,
'images' => json_encode($response->images),
'plans' => json_encode($response->plans),
'videos' => json_encode($response->videos),
'specialities' => json_encode($response->specialities),
'external_plans' => $response->external_plans,
'external_panoramas' => $response->external_panoramas,
'titles' => json_encode($response->titles),
'meta_keywords' => json_encode($response->meta_keywords),
'meta_descriptions' => json_encode($response->meta_descriptions),
'custom_urls' => json_encode($response->custom_urls),
'custom_titles' => json_encode($response->custom_titles),
'custom_keywords' => json_encode($response->custom_keywords),
'custom_descriptions' => json_encode($response->custom_descriptions),
'parking_facilities' => json_encode($response->parking_facilities),
'price_specification' => $response->price_specification,
'price_type_sale' => $response->price_type_sale,
'parking_capacity' => $response->parking_capacity,
'roof' => $response->roof,
'roof_material' => $response->roof_material,
'acceptance_date' => property_exists($response, 'acceptance_date') ? $response->acceptance_date : null,
'price_type_rent' => property_exists($response, 'price_type_rent') ? $response->price_type_rent : null,
'available_from' => property_exists($response, 'available_from') ? $response->available_from : null,
'rented_till' => property_exists($response, 'rented_till') ? $response->rented_till : null,
'min_contract_length' => property_exists($response, 'min_contract_length') ? $response->min_contract_length : null,
'contract_length' => property_exists($response, 'contract_length') ? $response->contract_length : null,
'rent_includes' => property_exists($response, 'rent_includes') ? $response->rent_includes : null,
'rental_price_specification' => property_exists($response, 'rental_price_specification') ? $response->rental_price_specification : null,
'renter_costs' => property_exists($response, 'renter_costs') ? $response->renter_costs : null,
'deposit' => property_exists($response, 'deposit') ? $response->deposit : null,
'availability' => property_exists($response, 'availability') ? $response->availability : null,
'rental_price' => property_exists($response, 'rental_price') ? $response->rental_price : null,
'rental_condition' => property_exists($response, 'rental_condition') ? $response->rental_condition : null,
'panorama_viewer' => property_exists($response, 'panorama_viewer') ? $response->panorama_viewer : null,
'garden' => property_exists($response, 'garden') ? $response->garden : '',
'garden_quality' => property_exists($response, 'garden_quality') ? $response->garden_quality : null,
'garden_total_surface' => property_exists($response, 'garden_total_surface') ? $response->garden_total_surface : null,
'garden_position' => property_exists($response, 'garden_position') ? $response->garden_position : null,
'garden_length' => property_exists($response, 'garden_length') ? $response->garden_length : null,
'garden_width' => property_exists($response, 'garden_width') ? $response->garden_width : null,
'garden_surface' => property_exists($response, 'garden_surface') ? $response->garden_surface : null,
'storage' => property_exists($response, 'storage') ? $response->storage : null,
'storage_total_amount' => property_exists($response, 'storage_total_amount') ? $response->storage_total_amount : null,
'storage_facilities' => property_exists($response, 'storage_facilities') ? $response->storage_facilities : null,
'storage_insulation' => property_exists($response, 'storage_insulation') ? $response->storage_insulation : null,
'garage' => property_exists($response, 'garage') ? $response->garage : null,
'garage_capacity' => property_exists($response, 'garage_capacity') ? $response->garage_capacity : null,
'garage_total_amount' => property_exists($response, 'garage_total_amount') ? $response->garage_total_amount : null,
'garage_length' => property_exists($response, 'garage_length') ? $response->garage_length : null,
'garage_width' => property_exists($response, 'garage_width') ? $response->garage_width : null,
'garage_surface' => property_exists($response, 'garage_surface') ? $response->garage_surface : null,
'garage_services' => property_exists($response, 'garage_services') ? $response->garage_services : null,
'garage_insulation' => property_exists($response, 'garage_insulation') ? $response->garage_insulation : null,
'short_description' => property_exists($response, 'short_description') ? $response->short_description : null,
'leaflet_text' => property_exists($response, 'leaflet_text') ? $response->leaflet_text : null,
'usps' => property_exists($response, 'usps') ? $response->usps : null,
]);
}

"我的对象"模型具有以下内容:

protected $casts = [
'featureFacilities' => 'array',
'land_registries' => 'array',
'featureFacilities' => 'array',
'locations' => 'array',
'views' => 'array',
'open_house_dates' => 'array',
'images' => 'array',
'plans' => 'array',
'videos' => 'array',
'specialities' => 'array',
'titles' => 'array',
'meta_keywords' => 'array',
'meta_descriptions' => 'array',
'custom_urls' => 'array',
'custom_titles' => 'array',
'custom_keywords' => 'array',
'custom_descriptions' => 'array',
'parking_facilities' => 'array',
'roof_material' => 'array',
];

它看起来像一个复杂的数组,你需要调试它。

$response->garage语法是否实际返回数据?

它是否在错误中显示它确切地崩溃了?


但是,我注意到您的代码的一些事情:

  1. 根据经验,永远不要在循环中调用Model::create(),尝试将其写入数组(或集合(中,并使用Model::insert()一次性写入数据库。

像这样:

$arr = [];
foreach ($responses as $response) {
$arr[] = [
'agency_id' => $agency->id,
'user_id' => $agency->user_id
];
}
Objects::insert($arr);

(有了这个$arr你可能会看到出了什么问题(

  1. 我认为这个语法property_exists($response, 'garage') ? $response->garage : null可以翻译成$response->garage ?? null

最新更新