Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search item of collection with key?

Tags:

php

laravel

I got the following collection:

$this->items = collect([$productId => [
                    'name' => $product->title,
                    'price' => $product->price,
                    'is_sale' => $product->is_sale,
                    'sale_price' => $product->sale_price,
                    'sale_percent' => $product->sale_percent,
                    'can_use_promocode' => $product->can_use_promocode,
                    'qty' => 1,
                ]);
]);

How to search an item with key? In documentation (https://laravel.com/docs/5.2/collections) I dont see any methods for it

UPD: For example, user added an item to cart ($this->items). I want to check existence of item in cart (need to do it with key). Analog for php function array_key_exists, but for collections.

like image 205
Alexxosipov Avatar asked Jan 20 '18 17:01

Alexxosipov


1 Answers

use has()

if($this->items->has('key_name_to_check')){
    ///your task if exists
}
like image 124
Sohel0415 Avatar answered Sep 29 '22 04:09

Sohel0415