Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shibbir's avatar

Laravel - Unable to cache the data

I want to cache some data in my Laravel Application. What I am doing is :

use Illuminate\Support\Facades\Cache;

$put = Cache::put('your_product', array(1,2,3,4), 3000 ); 

If I do var_dump I got false and Its not caching the data.

echo '<pre>';
    var_dump( $put );
echo '</pre>';

Is there another way to use the cache in laravel? Or I am doing wrong?

BTW, I am using latest laravel version.

0 likes
33 replies
Shibbir's avatar

@Sinnbeck Ok. Actually I don't need the return value but Its should be catch the data. So Cache::put(). is wrong here?

Sinnbeck's avatar

@Shibbir It does cache the data? If you want to get it from cache, later you do

echo '<pre>';
    var_dump( Cache::get('your_product') );
echo '</pre>';
Sinnbeck's avatar

@Shibbir I cannot recreate that

Cache::put('your_product', array(1,2,3,4), 3000 ); 
dd(Cache::get('your_product)); //shows 1,2,3,4 as an array
Shibbir's avatar

@Sinnbeck I understand but this really strange for me. It's showing me null :(

Shibbir's avatar

BTW, it's but vue + laravel application. When I get the product from the ajax call I use that put function to cache the data.

Sinnbeck's avatar

@Shibbir Did you do them right after each other like in my example? Or somewhere completely different?

tykus's avatar

@Shibbir what cache driver are you using? For example, the array cache driver lasts for the current request, so will not be there in subsequent requests (irrespective of the number of seconds!)

Shibbir's avatar

@Sinnbeck I use that putfunction when I get a product from the databse using a ajax call. Now I use the get function in another ajax call to see that output.

Shibbir's avatar

@tykus its default cache drive: 'default' => env('CACHE_DRIVER', 'file'),

Sinnbeck's avatar

@Shibbir So let me ask another way. Does it work if you do it the way I have shown in the same request?

Shibbir's avatar

@tykus So you mean I can not get the cache data in another request?

note: it's my vue + laravel application.

Shibbir's avatar

@Sinnbeck Opps! There is not CACHE_DRIVER key in my .env file. is this the reasone?

Sinnbeck's avatar

@Shibbir Yes you can. But I am trying to find out where it goes wrong. If you say it also does not work in the same request, we need to fix that, while if its only in different ajax calls it fails, we will focus on that.

Shibbir's avatar

@Sinnbeck Now, I think it's working after I added the key to the .env file, OH !

tykus's avatar

@Shibbir that doesn't make sense really; what value did you give for the CACHE_DRIVER key?

tykus's avatar

@Shibbir that has changed nothing; as @sinnbeck mentioned above it defaulted to file when there was no key in the .env file.

Shibbir's avatar

@tykus Just tested again after delete that key from the .env file. Now it's not working. I need to add the key to the.env file.

Sinnbeck's avatar

@Shibbir We cannot say. But changing it from file to file does nothing. So something else I would assume. If you remove it, does it start breaking again?

Sinnbeck's avatar

Are you sure 'default' => env('CACHE_DRIVER', 'file'), comes from /config/cache.php ?

Sinnbeck's avatar

@Shibbir Ok try removing it from .env and do dd(config('cache.default'));

What does this give you?

Sinnbeck's avatar

@Shibbir Ok I really dont understand what is going on with your project. Are you caching your config or something? php artisan config:clear

I cannot recreate it. Anyways: make sure to set it in .env and you are good I suppose

Sinnbeck's avatar

Just making sure.. Its not that you have this in your env when you "remove it"?

CACHE_DRIVER=

Please or to participate in this conversation.