settings)) { if (isset($self->services)) { $settings = $self->settings; $settings['services'] = $self->services ?? []; $self->settings = $settings; unset($self->services); } else { if (! empty($self->id)) { $self->tap(function ($query) use ($self) { /** @var $query self */ $exist = $query->find($self->id, ['settings'])->toArray(); if (isset($exist['settings']['services'])) { $settings = $self->settings; $settings['services'] = $exist['settings']['services']; $self->settings = $settings; } }); } } } if (isset($self->prices) && is_array($self->prices)) { $prices = $self->prices; ItemServicePrices::where('service_key', '=', $self->service_key)->delete(); $i = 0; foreach ($prices as $v) { $i++; $num = 1; $s = [ 'id' => $i, 'price' => $v['price'], ]; foreach ($v['cats'] as $c) { $s['category_id'] = $c; if (! empty($v['regions'])) { foreach ($v['regions'] as $r) { $s['region_id'] = $r; $s['num'] = $num++; $self->prices()->create($s); } } else { $s['num'] = $num++; $self->prices()->create($s); } } } unset($self->prices); } }); } /** * Price extra (regions, ...) * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function prices() { return $this->hasMany(ItemServicePrices::class, 'service_key', 'service_key'); } /** * @param mixed $id record id * @param array $opts * @return static|array */ public function onAdminFormLoad($id, array $opts = []) { $opts['with'][] = 'prices'; $data = parent::onAdminFormLoad($id, $opts); $data['services'] = $data['settings']['services'] ?? []; unset($data['settings']['services']); if (isset($data['prices'])) { $prices = []; foreach ($data['prices'] as $v) { if (empty($v['category_id'])) { continue; } if (isset($prices[$v['id']])) { $i = & $prices[$v['id']]; if (! in_array($v['category_id'], $i['cats'])) { $i['cats'][] = $v['category_id']; } if (! empty($v['region_id']) && ! in_array($v['region_id'], $i['regions'])) { $i['regions'][] = $v['region_id']; } } else { $prices[ $v['id'] ] = [ 'price' => $v['price'], 'cats' => [$v['category_id']], 'regions' => ! empty($v['region_id']) ? [$v['region_id']] : [], ]; } } $data['prices'] = $prices; } return $data; } }