key does not store in cache laravel

Viewed 887

here I'm trying to store key

$code = mt_rand(111111, 999999);
Cache::put('notification', $code, 600);

and in another function I try to get that key in this way

if(Cache::has('notification') and Cache::get('notification') == $request->code)
   return response()->json(['status' => true, 'group' => $request->res['group']]);
else
   return response()->json(['status' => false, 'message' => 'کد اشتباه است']);

but it always return false

1 Answers

The problem might be when you are using Cache::get() you are not storing the value anywhere. Please try to store it in a variable and compare the variable with $request->code.

$notification_cache = Cache::get('notification');
if (Cache::has('notification') and $notification_cache == $request->code)
    return response()->json(['status' => true, 'group' => $request->res['group']]);
else
    return response()->json(['status' => false, 'message' => 'کد اشتباه است']);
Related