From 118b9bb74c8f1db812f8693e7be567d463fe9ce1 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Mon, 19 Jun 2023 21:52:48 +0300 Subject: [PATCH] Add methods to City model to get, add, remove Companies by ids. --- app/Models/City.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/Models/City.php b/app/Models/City.php index 160ecfd..f9604f6 100644 --- a/app/Models/City.php +++ b/app/Models/City.php @@ -21,6 +21,24 @@ class City extends Model return $this->belongsToMany(Company::class, 'cities_companies', 'city_id', 'company_id'); } + public function getCompanyIds(): array + { + return $this->companies()->pluck('company_id')->toArray(); + } + + + public function addCompanyIds(array $company_ids) + { + $companyIDs = array_merge($this->getCompanyIds(), $company_ids); + $this->companies()->sync($companyIDs); + } + + + public function removeCompanyIds(array $company_ids) + { + $this->companies()->detach($company_ids); + } + public static function validate_with_name(string $name) { $name = $name ?? '';