From a7dbea1d79b1607c8ce3c685772fad9e332ef7a8 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Mon, 19 Jun 2023 15:31:33 +0300 Subject: [PATCH] Add getCategory, getCompany, isBelong methods to Item model. --- app/Models/Item.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/app/Models/Item.php b/app/Models/Item.php index 3443036..4ef4d5d 100644 --- a/app/Models/Item.php +++ b/app/Models/Item.php @@ -31,6 +31,40 @@ class Item extends Model return $this->belongsToMany(Cart::class, 'carts_items', 'item_id', 'cart_id'); } + public function getCategory(): Category + { + $category = Category::where('id', $this->category_id)->first(); + + return $category; + } + + public function getCompany() + { + $category = $this->getCategory(); + + if($category == null) + return null; + + $company = Company::where('id', $category->company_id)->first(); + + return $company; + } + + public function isBelong(Company $company): bool + { + $category = $this->getCategory(); + + if($category == null) + return false; + + $its_company = $this->getCompany(); + + if($its_company == null or $company == null) + return false; + + return $its_company->id == $company->id; + } + public static function validate_with_name(string $name) { $name = $name ?? '';