Laravel: check if cache has key with thag

33 views Asked by At

I have cached something with tags:

Cache::tags(['mytag'])->put('topmenu', $dataMenus);

Was just reading the docs, and i can check the cache for a key:

if(Cache::has('topmenu')) {
    // ....
}

But how do I check the key AND the tags? I would think like this:

if(Cache::tags(['mytag'])->has('topmenu')) {
    // ...
}

But I am not sure, what if the tags doesn't exist?

1

There are 1 answers

1
Sandalanka On

I had same problem.

I solved this problem.

$tag = 'mytag'; $key = 'topmenu';

$value = collect(Cache::tags($tag)->getTags()->flush())->first(fn($item) => $item === $key);

now you check use if condition value empty or not check.