'boolean', ]; /** * The attributes that should be mutated to dates. * * @var array */ protected $dates = ['deleted_at']; /** * The attributes that are translatable. * * @var array */ protected array $translatedAttributes = ['name']; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::saved(function ($variation) { if (request()->routeIs('admin.variations.*')) { $variation->saveValuesForGlobal(); } if (request()->routeIs('admin.products.*')) { $variation->saveValuesForLocal(); } }); } /** * Save values for the variation. * * @param array $values * * @return void */ public function saveValues(array $values = []): void { $ids = $this->getDeleteCandidates($values); if ($ids->isNotEmpty()) { $this->values() ->whereIn('id', $ids) ->delete(); } $counter = 0; foreach (array_reset_index($values) as $attributes) { $attributes += ['position' => ++$counter]; $attributes += ['value' => $attributes['color'] ?? '']; $this->values()->updateOrCreate( [ 'id' => array_get($attributes, 'id'), ], $attributes, ); } } /** * Get the values for the variation. * * @return HasMany */ public function values(): HasMany { return $this->hasMany(VariationValue::class); } /** * Scope a query to only include global variations. * * @param Builder $query * * @return Builder */ public function scopeGlobals(Builder $query): Builder { return $query->where('is_global', true); } /** * Get table data for the resource * * @return VariationTable */ public function table(): VariationTable { return new VariationTable($this->newQuery()->globals()); } protected function saveValuesForGlobal() { $this->saveValues(request('values', [])); } protected function saveValuesForLocal() { $this->saveValues( request('variations.' . $this->uid . '.values', []) ); } /** * @param $values * * @return Collection */ private function getDeleteCandidates($values): Collection { return $this->values() ->pluck('id') ->diff(array_pluck($values, 'id')); } }