From a8559a52c669ccb7b2c54fa1f956e9f059f017d4 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 18 Jun 2023 18:34:19 +0300 Subject: [PATCH] Add the setCompany method in the Cart model. --- app/Models/Cart.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/Models/Cart.php b/app/Models/Cart.php index af601ee..c591733 100644 --- a/app/Models/Cart.php +++ b/app/Models/Cart.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsTo; use App\Models\City; +use App\Models\Company; use Brick\Math\BigInteger; class Cart extends Model @@ -52,7 +53,21 @@ class Cart extends Model $this->save(); } - public function addItem(BigInteger $item) { + 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 addItem(BigInteger $item) + { $this->items()->sync($item); }