From aedb4d5949f0c458a9e3e5f587eb68f9702deacc Mon Sep 17 00:00:00 2001 From: KKlochko Date: Mon, 19 Jun 2023 15:10:47 +0300 Subject: [PATCH] Add the validation for Item that returns message for requests. --- app/Models/Item.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/Models/Item.php b/app/Models/Item.php index 5412d37..3443036 100644 --- a/app/Models/Item.php +++ b/app/Models/Item.php @@ -30,5 +30,26 @@ class Item extends Model { return $this->belongsToMany(Cart::class, 'carts_items', 'item_id', 'cart_id'); } + + public static function validate_with_name(string $name) + { + $name = $name ?? ''; + + if($name == '') + return [ + 'error' => 'The item name is empty, please, write the name!!!' + ]; + + $item = Item::where('name', $name)->first(); + + if(!$item) + return [ + 'error' => 'A item with the name does not exist!!!' + ]; + + return [ + 'ok' => 'A item with the name is valid.' + ]; + } }