From 563254c7251c1a5dedae3bb62c55e8d5ff0ca732 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Mon, 19 Jun 2023 18:11:28 +0300 Subject: [PATCH] Add getItem to Cart model. --- app/Models/Cart.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/Models/Cart.php b/app/Models/Cart.php index 46ccbca..b9ee7a8 100644 --- a/app/Models/Cart.php +++ b/app/Models/Cart.php @@ -46,6 +46,17 @@ class Cart extends Model return $this->belongsToMany(Item::class, 'carts_items', 'cart_id', 'item_id'); } + public function getItem(string $name): Item + { + $itemsIds = $this->getItemIds(); + + $itemId = Item::whereIn('id', $itemsIds) + ->where('uuid', $name) + ->count(); + + return Item::where('id', $itemId)->first(); + } + public function getItemIds(): array { return $this->items()->pluck('item_id')->toArray();