Add getCategory, getCompany, isBelong methods to Item model.

main
KKlochko 2 years ago
parent 751921e83f
commit a7dbea1d79

@ -31,6 +31,40 @@ class Item extends Model
return $this->belongsToMany(Cart::class, 'carts_items', 'item_id', 'cart_id'); 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) public static function validate_with_name(string $name)
{ {
$name = $name ?? ''; $name = $name ?? '';

Loading…
Cancel
Save