belongsTo(City::class); } public function getCity(): City { return City::where('city_id', $this->city_id)->first(); } public function company(): BelongsTo { return $this->belongsTo(Company::class); } public function getCompany(): Company { return Company::where('company_id', $this->company_id)->first(); } public function items(): BelongsToMany { return $this->belongsToMany(Item::class, 'carts_items', 'cart_id', 'item_id'); } public function getItemIds(): array { return $this->items()->pluck('item_id')->toArray(); } public function isEmpty() { return count($this->getItemIds()); } public function dropItems() { $this->items()->detach(); } public function setCity(City $city) { if($this->city_id == $city->id) return; $this->city_id = $city->id; if(!$this->isEmpty()) $this->dropItems(); $this->save(); } public function setCompany(Company $company) { if($this->company_id == $company->id) return; $this->company_id = $company->id; if(!$this->isEmpty()) $this->dropItems(); $this->save(); } public function addItemId(int $item) { $this->items()->sync($item); } public function addItemIds(array $items) { $this->items()->sync($items); } public function removeItemId(int $item) { $this->items()->sync($item); } public function removeItemIds(array $items) { $this->items()->sync($items); } }